Skip to contentSkip to conversation

error: Automatic error concatenation and prefixing

RFC PATCH In review Peter Crosthwaite 4 participants 36 messages Sep 11, 2015 – Sep 14, 2015

Subsystems (5)
lore
Share & tools
lore
#1 lore raw

RFC PATCH 1/25 exec: convert error_report to error_report_err

In reply to Peter Crosthwaite

This is a standard error_report_err. Convert it.

---

 exec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
exec.c +1 -1
index 31d2dc7..1e7fad8 100644
@@ -1241,7 +1241,7 @@ static void *file_ram_alloc(RAMBlock *block,
 
 error:
     if (mem_prealloc) {
-        error_report("%s", error_get_pretty(*errp));
+        error_report_err(*errp);
         exit(1);
     }
     return NULL;
Signature · 1 line
1.9.1
#3 lore raw

RFC PATCH 2/25 s390x: virtio-ccw: Remove un-needed if guard

In reply to Peter Crosthwaite

error_propagate is already NULL safe. Remove these un-needed if guards.

Cc Richard Henderson <[email protected]>
Cc Alexander Graf <[email protected]>

---

 hw/s390x/virtio-ccw.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
hw/s390x/virtio-ccw.c +7 -21
index d36373e..db085bc 100644
@@ -957,9 +957,7 @@ static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
                                   object_get_typename(OBJECT(qdev)));
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void virtio_ccw_net_instance_init(Object *obj)
@@ -980,9 +978,7 @@ static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void virtio_ccw_blk_instance_init(Object *obj)
@@ -1017,9 +1013,7 @@ static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 
@@ -1039,9 +1033,7 @@ static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void virtio_ccw_balloon_instance_init(Object *obj)
@@ -1077,9 +1069,7 @@ static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void virtio_ccw_scsi_instance_init(Object *obj)
@@ -1101,9 +1091,7 @@ static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void vhost_ccw_scsi_instance_init(Object *obj)
@@ -1891,9 +1879,7 @@ static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 
     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-    }
+    error_propagate(errp, err);
 }
 
 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
Signature · 1 line
1.9.1
#0 lore raw

cover error: Automatic error concatenation and prefixing

Hi Markus and all,

This patch series adds support for automatically concatenating multiple errors to the one Error *.

I'll start with what I am actually trying to do, which is get rid of the boilerplate:

some_api_call(... , &local_err);

if (local_err) {
    error_propagate(errp, local_err);
    return;
}
some_similar_api_call(... , &local_err);
if (local_err) {
    error_propagate(errp, local_err);
    return;
}
some_similar_api_call(... , &local_err);
if (local_err) {
    error_propagate(errp, local_err);
    return;
}

...

It is usually 1 LOC for the API and 4 LOC for the error handling boiler plate making our code hard reading. This is particularly bad in hw/arm where we have a good number of fully QOMified SoCs and machine models, each of which need these checks on recursive realize calls and friends.

The removed LOC just in ARM pays for the extra core code needed to implement this. And the number of ARM machines is only going to grow.

So the plan is:

1: Allow an Error * to contain more that one actual error from API calls.
2: Refactor key APIs (some_similar_api_call() in the above example) to not fatal when previous errors have occured in dependencies.

Point 1 kind of got big on me. Patch 4 is the main event, listifying errors. The follow up issue however, is it tricky to get a sane definition of error_get_pretty for a multi-error. So instead the strategy is to remove error_get_pretty() and replace with some error API helper with well defined behaviour for multi-error. The two leading uses of error get pretty are prefixing an error, and reporting an error via a custom printf handler. So two new APIs are added for that (P5-6). There aren't many error_get_pretty's left after that, and they shouldn't be in the path of any multi-errors.

I think the error_prefix is valuable it its own right, as it now means the code for report or propagating a prefixed error is now consistent with the non-prefixed variants.

That is, we used to have:

/* If we are prefixing we have to do it this way: */ error_setg(errp, "some prefix %s", error_get_pretty(local_err)); error_free(local_err);

vs:

/* but if not prefixing it is like this: */
error_propagate(errp, local_err);

Now with this patch series the two are much more recognisable as the same with:

/* This code is almost the same as the above non-prefixed propagation */ error_prefix(local_err, "some prefix"):
error_propagate(errp, local_err);

Point 2 is less about error API and more about machine generation. Sysbus, Memory and Qdev all have APIs that depend on successful device- init and realize calls for argument devices. As we are trying to remove the error detection for those argument devs, those APIs need to tweaked to handle realize failure. This actually wasn't as bad as I thought it would be. See patches (7-9).

All patches after that walk the various major subsystems converting error APIs to this new scheme and deleting now-unneeded boiler plate. ARM is first (P10-15) seeing good clean up of propagate handers.

So the net result for these ARM machines, is error behaviour that is something like a compiler. If any one thing fails, then machine-init (compilation) fails. But an early fail does not stop machine-init (compilation), instead it proceeds to the end collecting subsequent errors as it goes.

Some other interesting food for thought is the qemu_fdt APIs which I have been wanting to convert to error API but the local_err propagation is going to be brutal in heavy users like e500.c. This would solve that as fdt API could be easily made multi-error safe and clients like e500 can just collect multi-errors and single-fail at the end.

Long term, we can use this to catch cases of multiple genuine machine init errors in the one boot but that is a secondary goal to simply cutting down on code boilerplate. The best feature of this series is the diffstat.

Patches 1-3 are cleanup that can be taken independent of the series.

I think P3 may be obsolete from a recent merge, but i'll wait for architectural feedback before rebasing.

Regards,
Peter
--END---

---
 __HAS_COVER__ | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 __HAS_COVER__
__HAS_COVER__ new file +0 -0
new file mode 100644
index 0000000..e69de29
Signature · 83 lines
1.9.1



Peter Crosthwaite (25):
  exec: convert error_report to error_report_err
  s390x: virtio-ccw: Remove un-needed if guard
  error: Factor out common error setter logic
  error: Add support for multiple errors
  error: Add error prefix API
  error: Add error_printf_fn()
  sysbus: mmio_map+mmio_get_region: ignore range OOB errors
  memory: nop APIs when they have NULL arguments
  qdev: gpio: Ignore unconnectable GPIOs
  arm: xlnx-zynqmp: Update error API usages
  arm: fsl-imx*: Update error API usages
  arm: netduino: Update error API usages
  arm: allwinner: Update error API usages
  arm: digic: Update error API usages
  cpu: arm: Remove un-needed error checking
  ppc: Update error API usages
  i386: pc: Update error API usages
  monitor: update error API usages
  qdev: Update error API usages
  block: Update error API usages
  tests: Update error API usages
  usb: bus: Update error API usages
  scsi: Update error API usages
  migration: savevm: Update error API usages
  core: Update error API usages

 arch_init.c                     |   5 +-
 block.c                         |   5 +-
 block/qcow2.c                   |   5 +-
 block/qed.c                     |   5 +-
 block/sheepdog.c                |   8 +--
 blockdev.c                      |  10 +--
 exec.c                          |   2 +-
 hmp.c                           |  33 ++++-----
 hw/arm/allwinner-a10.c          |  18 +----
 hw/arm/cubieboard.c             |  28 +++-----
 hw/arm/digic.c                  |  19 +----
 hw/arm/digic_boards.c           |   4 +-
 hw/arm/fsl-imx25.c              |  46 +-----------
 hw/arm/fsl-imx31.c              |  42 +----------
 hw/arm/netduino2.c              |   2 +-
 hw/arm/stm32f205_soc.c          |  14 +---
 hw/arm/xilinx_zynq.c            |  14 +---
 hw/arm/xlnx-ep108.c             |   2 +-
 hw/arm/xlnx-zynqmp.c            |  29 +-------
 hw/block/dataplane/virtio-blk.c |   5 +-
 hw/core/qdev-properties.c       |   7 +-
 hw/core/qdev.c                  |  15 ++--
 hw/core/sysbus.c                |  11 ++-
 hw/cpu/a15mpcore.c              |   6 +-
 hw/cpu/a9mpcore.c               |  22 +-----
 hw/cpu/arm11mpcore.c            |  18 +----
 hw/cpu/realview_mpcore.c        |   9 +--
 hw/i386/pc.c                    |   5 +-
 hw/ppc/e500.c                   |   4 +-
 hw/ppc/spapr.c                  |   4 +-
 hw/ppc/spapr_drc.c              |   6 +-
 hw/s390x/virtio-ccw.c           |  28 ++------
 hw/scsi/vhost-scsi.c            |   5 +-
 hw/usb/bus.c                    |  10 ++-
 include/monitor/monitor.h       |   2 +-
 include/qapi/error.h            |  13 ++++
 memory.c                        |   9 +++
 migration/savevm.c              |  22 +++---
 monitor.c                       |  11 ++-
 qapi/common.json                |   5 +-
 qemu-img.c                      |  38 +++++-----
 stubs/mon-printf.c              |   2 +-
 tests/test-aio.c                |   5 +-
 tests/test-thread-pool.c        |   5 +-
 tests/test-throttle.c           |   9 ++-
 ui/vnc.c                        |   5 +-
 util/error.c                    | 154 ++++++++++++++++++++++++++--------------
 vl.c                            |   7 +-
 48 files changed, 281 insertions(+), 452 deletions(-)

-- 
1.9.1
#6 lore raw

RFC PATCH 3/25 error: Factor out common error setter logic

Currently set_error, set_error_errno, set_error_win32 do pretty much the same thing with only a little bit of string manipulation in the error code cases. Coreify that string manipulation as a callback and factor out the bulk of the error_set fns to a common helper that calls the callback at the right time.

---

 util/error.c | 81 +++++++++++++++++++++++++-----------------------------------
 1 file changed, 33 insertions(+), 48 deletions(-)
util/error.c +33 -48
index 14f4351..b93e5c8 100644
@@ -22,7 +22,9 @@ struct Error
 
 Error *error_abort;
 
-void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
+static void do_error_set(Error **errp, ErrorClass err_class,
+                         void (*mod)(Error *, void *), void *mod_opaque,
+                         const char *fmt, ...)
 {
     Error *err;
     va_list ap;
@@ -38,6 +40,9 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
     va_start(ap, fmt);
     err->msg = g_strdup_vprintf(fmt, ap);
     va_end(ap);
+    if (mod) {
+        mod(err, mod_opaque);
+    }
     err->err_class = err_class;
 
     if (errp == &error_abort) {
@@ -50,40 +55,33 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
     errno = saved_errno;
 }
 
-void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
-                     const char *fmt, ...)
+void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
 {
-    Error *err;
-    char *msg1;
     va_list ap;
-    int saved_errno = errno;
 
-    if (errp == NULL) {
-        return;
-    }
-    assert(*errp == NULL);
+    va_start(ap, fmt);
+    do_error_set(errp, err_class, NULL, NULL, fmt, ap);
+    va_end(ap);
+}
 
-    err = g_malloc0(sizeof(*err));
+static void error_set_errno_mod(Error *err, void *opaque) {
+    int os_errno = *(int *)opaque;
+    char *msg1 = err->msg;
 
-    va_start(ap, fmt);
-    msg1 = g_strdup_vprintf(fmt, ap);
     if (os_errno != 0) {
         err->msg = g_strdup_printf("%s: %s", msg1, strerror(os_errno));
         g_free(msg1);
-    } else {
-        err->msg = msg1;
-    }
-    va_end(ap);
-    err->err_class = err_class;
-
-    if (errp == &error_abort) {
-        error_report_err(err);
-        abort();
     }
+}
 
-    *errp = err;
+void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
+                     const char *fmt, ...)
+{
+    va_list ap;
 
-    errno = saved_errno;
+    va_start(ap, fmt);
+    do_error_set(errp, err_class, error_set_errno_mod, &os_errno, fmt, ap);
+    va_end(ap);
 }
 
 void error_setg_file_open(Error **errp, int os_errno, const char *filename)
@@ -93,40 +91,27 @@ void error_setg_file_open(Error **errp, int os_errno, const char *filename)
 
 #ifdef _WIN32
 
-void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
-                     const char *fmt, ...)
-{
-    Error *err;
-    char *msg1;
-    va_list ap;
+static void error_set_win32_mod(Error *err, void *opaque) {
+    int win32_err = *(int *)opaque;
+    char *msg1 = err->msg;
 
-    if (errp == NULL) {
-        return;
-    }
-    assert(*errp == NULL);
-
-    err = g_malloc0(sizeof(*err));
-
-    va_start(ap, fmt);
-    msg1 = g_strdup_vprintf(fmt, ap);
     if (win32_err != 0) {
         char *msg2 = g_win32_error_message(win32_err);
         err->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
                                    (unsigned)win32_err);
         g_free(msg2);
         g_free(msg1);
-    } else {
-        err->msg = msg1;
     }
-    va_end(ap);
-    err->err_class = err_class;
+}
 
-    if (errp == &error_abort) {
-        error_report_err(err);
-        abort();
-    }
+void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
+                     const char *fmt, ...)
+{
+    va_list ap;
 
-    *errp = err;
+    va_start(ap, fmt);
+    do_error_set(errp, err_class, error_set_win32_mod, &win32_err, fmt, ap);
+    va_end(ap);
 }
 
 #endif
Signature · 1 line
1.9.1
#8 lore raw

RFC PATCH 4/25 error: Add support for multiple errors

In reply to Peter Crosthwaite

Allow errors to stack. If an error is already set, don't assert, instead, form a linked list. Recent errors are at the front of the list, older ones at the back.

The assertion against the destination erro already being set is removed.

copy/free are all to call their functionality recursively.

Propagation is implemented as concatenation of two error lists.

---

 qapi/common.json |  5 ++++-
 util/error.c     | 27 +++++++++++++++++++++------
 2 files changed, 25 insertions(+), 7 deletions(-)
qapi/common.json +4 -1
index bad56bf..04175db 100644
@@ -22,11 +22,14 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
+# @MultipleErrors: Multiple errors have occured
+#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
+            'MultipleErrors' ] }
 
 ##
 # @VersionTriple
util/error.c +21 -6
index b93e5c8..890ce58 100644
@@ -18,28 +18,25 @@ struct Error
 {
     char *msg;
     ErrorClass err_class;
+    struct Error *next;
 };
 
 Error *error_abort;
 
 static void do_error_set(Error **errp, ErrorClass err_class,
                          void (*mod)(Error *, void *), void *mod_opaque,
-                         const char *fmt, ...)
+                         const char *fmt, va_list ap)
 {
     Error *err;
-    va_list ap;
     int saved_errno = errno;
 
     if (errp == NULL) {
         return;
     }
-    assert(*errp == NULL);
 
     err = g_malloc0(sizeof(*err));
 
-    va_start(ap, fmt);
     err->msg = g_strdup_vprintf(fmt, ap);
-    va_end(ap);
     if (mod) {
         mod(err, mod_opaque);
     }
@@ -50,6 +47,7 @@ static void do_error_set(Error **errp, ErrorClass err_class,
         abort();
     }
 
+    err->next = *errp;
     *errp = err;
 
     errno = saved_errno;
@@ -120,15 +118,22 @@ Error *error_copy(const Error *err)
 {
     Error *err_new;
 
+    if (!err) {
+        return NULL;
+    }
     err_new = g_malloc0(sizeof(*err));
     err_new->msg = g_strdup(err->msg);
     err_new->err_class = err->err_class;
+    err_new->next = error_copy(err->next);
 
     return err_new;
 }
 
 ErrorClass error_get_class(const Error *err)
 {
+    if (err->next) {
+        return ERROR_CLASS_MULTIPLE_ERRORS;
+    }
     return err->err_class;
 }
 
@@ -145,6 +150,9 @@ void error_report_err(Error *err)
 
 void error_free(Error *err)
 {
+    if (err->next) {
+        error_free(err->next);
+    }
     if (err) {
         g_free(err->msg);
         g_free(err);
@@ -156,8 +164,15 @@ void error_propagate(Error **dst_errp, Error *local_err)
     if (local_err && dst_errp == &error_abort) {
         error_report_err(local_err);
         abort();
-    } else if (dst_errp && !*dst_errp) {
+    } else if (dst_errp) {
+        Error *i;
+        Error *old_dst_err = *dst_errp;
+
         *dst_errp = local_err;
+        for (i = local_err; i; i = i->next) {
+            dst_errp = &i->next;
+        }
+        *dst_errp = old_dst_err;
     } else if (local_err) {
         error_free(local_err);
     }
Signature · 1 line
1.9.1
#10 lore raw

RFC PATCH 5/25 error: Add error prefix API

In reply to Peter Crosthwaite

Add an API to prefix an already set error with a caller-centric message.

If multiple errors are set, all are prefixed individually.

---

 include/qapi/error.h |  6 ++++++
 util/error.c         | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/qapi/error.h +6 -0
index f44c451..b25c72f 100644
@@ -78,6 +78,12 @@ ErrorClass error_get_class(const Error *err);
 Error *error_copy(const Error *err);
 
 /**
+ * Prefix an error message with a formatted string.
+ */
+
+void error_prefix(Error *err, const char *fmt, ...);
+
+/**
  * Get a human readable representation of an error object.
  */
 const char *error_get_pretty(Error *err);
util/error.c +26 -0
index 890ce58..e9c23ce 100644
@@ -19,6 +19,7 @@ struct Error
     char *msg;
     ErrorClass err_class;
     struct Error *next;
+    bool prefixed;
 };
 
 Error *error_abort;
@@ -142,6 +143,26 @@ const char *error_get_pretty(Error *err)
     return err->msg;
 }
 
+void error_prefix(Error *err, const char *fmt, ...) {
+    char *msg;
+    char *fmt_full;
+    va_list ap;
+
+    if (!err || err->prefixed) {
+        return;
+    }
+    err->prefixed = true;
+
+    msg = err->msg;
+    fmt_full =  g_strdup_printf("%s%%s", fmt);
+
+    va_start(ap, fmt);
+    err->msg = g_strdup_printf(fmt_full, ap, msg);
+    va_end(ap);
+    g_free(fmt_full);
+    g_free(msg);
+}
+
 void error_report_err(Error *err)
 {
     error_report("%s", error_get_pretty(err));
@@ -170,6 +191,11 @@ void error_propagate(Error **dst_errp, Error *local_err)
 
         *dst_errp = local_err;
         for (i = local_err; i; i = i->next) {
+            /* Propagation implies that the caller is no longer the owner of the
+             * error. Therefore reset prefixes, so higher level handlers can
+             * prefix again.
+             */
+            i->prefixed = false;
             dst_errp = &i->next;
         }
         *dst_errp = old_dst_err;
Signature · 1 line
1.9.1
#12 lore raw

RFC PATCH 6/25 error: Add error_printf_fn()

In reply to Peter Crosthwaite

Add an API to report an error with a custom printf function. Use this for the implementation of error_report_err().

---

 include/qapi/error.h |  7 +++++++
 util/error.c         | 22 ++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)
include/qapi/error.h +7 -0
index b25c72f..0e5c311 100644
@@ -94,6 +94,13 @@ const char *error_get_pretty(Error *err);
 void error_report_err(Error *);
 
 /**
+ * Report an and free an error object using a custom printf implementation.
+ */
+
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque);
+
+/**
  * Propagate an error to an indirect pointer to an error.  This function will
  * always transfer ownership of the error reference and handles the case where
  * dst_err is NULL correctly.  Errors after the first are discarded.
util/error.c +20 -2
index e9c23ce..c4656af 100644
@@ -163,10 +163,18 @@ void error_prefix(Error *err, const char *fmt, ...) {
     g_free(msg);
 }
 
+static void error_report_err_printf(void *opaque, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vreport(fmt, ap);
+    va_end(ap);
+}
+
 void error_report_err(Error *err)
 {
-    error_report("%s", error_get_pretty(err));
-    error_free(err);
+    error_printf_fn(err, error_report_err_printf, NULL);
 }
 
 void error_free(Error *err)
@@ -180,6 +188,16 @@ void error_free(Error *err)
     }
 }
 
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque)
+{
+    if (err->next) {
+        error_printf_fn(err->next, printf_fn, printf_opaque);
+    }
+    printf_fn(printf_opaque, "%s\n", error_get_pretty(err));
+    error_free(err);
+}
+
 void error_propagate(Error **dst_errp, Error *local_err)
 {
     if (local_err && dst_errp == &error_abort) {
Signature · 1 line
1.9.1
#14 lore raw

RFC PATCH 7/25 sysbus: mmio_map+mmio_get_region: ignore range OOB errors

In reply to Peter Crosthwaite

Ignore these errors as they may be a follow on effect of device realisation failure. Ideally we should have an error ** to populate in own right, but that requires an API change. Mark FIXME.

---

 hw/core/sysbus.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/core/sysbus.c +10 -1
index 3c58629..6fde10e 100644
@@ -127,7 +127,12 @@ bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
                                    bool may_overlap, int priority)
 {
-    assert(n >= 0 && n < dev->num_mmio);
+    assert(n >= 0);
+
+    if (n < dev->num_mmio) {
+        /* FIXME: Add an Error ** to this function for this condition */
+        return;
+    }
 
     if (dev->mmio[n].addr == addr) {
         /* ??? region already mapped here.  */
@@ -186,6 +191,10 @@ void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
 
 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
 {
+    if (n >= dev->num_mmio) {
+        /* FIXME: Add an Error ** to this function for this condition */
+        return NULL;
+    }
     return dev->mmio[n].memory;
 }
 
Signature · 1 line
1.9.1
#15 lore raw

RFC PATCH 8/25 memory: nop APIs when they have NULL arguments

In reply to Peter Crosthwaite

When doing a subregion or alias and the SR/Aliased region is NULL, perform no action. This makes the memory API tolerant of API calls following an earlier failure with setting up these dependencies. This in turn allows removal of some of the constant error-checking boiler-plate from machine models.

---

 memory.c | 9 +++++++++
 1 file changed, 9 insertions(+)
memory.c +9 -0
index 0d8b2d9..2d03cb7 100644
@@ -1266,6 +1266,9 @@ void memory_region_init_alias(MemoryRegion *mr,
                               hwaddr offset,
                               uint64_t size)
 {
+    if (!orig) {
+        return;
+    }
     memory_region_init(mr, owner, name, size);
     mr->alias = orig;
     mr->alias_offset = offset;
@@ -1759,6 +1762,9 @@ void memory_region_add_subregion(MemoryRegion *mr,
                                  hwaddr offset,
                                  MemoryRegion *subregion)
 {
+    if (!subregion) {
+        return;
+    }
     subregion->may_overlap = false;
     subregion->priority = 0;
     memory_region_add_subregion_common(mr, offset, subregion);
@@ -1769,6 +1775,9 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,
                                          MemoryRegion *subregion,
                                          int priority)
 {
+    if (!subregion) {
+        return;
+    }
     subregion->may_overlap = true;
     subregion->priority = priority;
     memory_region_add_subregion_common(mr, offset, subregion);
Signature · 1 line
1.9.1
#16 lore raw

RFC PATCH 9/25 qdev: gpio: Ignore unconnectable GPIOs

In reply to Peter Crosthwaite

Nop rather than assert if a GPIO connection cannot be made. This allows for continuation of machine init after an error which allows removal of error detection boilerplate and detection of subsequent errors.

The actual error (i.e. a GPIO endpoint was not inited) will have already been raised by other APIs.

Ideally this API should raise an error in its own right. Add a FIXME.

---

 hw/core/qdev.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/core/qdev.c +7 -2
index b2f404a..52140ba 100644
@@ -467,7 +467,11 @@ qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
 {
     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
 
-    assert(n >= 0 && n < gpio_list->num_in);
+    assert(n >= 0);
+    if (n >= gpio_list->num_in) {
+        /* FIXME: add error ** to this API */
+        return NULL;
+    }
     return gpio_list->in[n];
 }
 
@@ -491,7 +495,8 @@ void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
                                                 "/unattached"),
                                   "non-qdev-gpio[*]", OBJECT(pin), NULL);
     }
-    object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
+    /* FIXME: add error ** to this API */
+    object_property_set_link(OBJECT(dev), OBJECT(pin), propname, NULL);
     g_free(propname);
 }
 
Signature · 1 line
1.9.1
#17 lore raw

RFC PATCH 10/25 arm: xlnx-zynqmp: Update error API usages

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures. Use error_report_err as appropriate.

---

 hw/arm/xilinx_zynq.c | 14 +-------------
 hw/arm/xlnx-ep108.c  |  2 +-
 hw/arm/xlnx-zynqmp.c | 29 +----------------------------
 3 files changed, 3 insertions(+), 42 deletions(-)
hw/arm/xilinx_zynq.c +1 -13
index a4e7b5c..de2be6a 100644
@@ -132,24 +132,12 @@ static void zynq_init(MachineState *machine)
      */
     if (object_property_find(OBJECT(cpu), "has_el3", NULL)) {
         object_property_set_bool(OBJECT(cpu), false, "has_el3", &err);
-        if (err) {
-            error_report_err(err);
-            exit(1);
-        }
     }
 
     object_property_set_int(OBJECT(cpu), ZYNQ_BOARD_MIDR, "midr", &err);
-    if (err) {
-        error_report_err(err);
-        exit(1);
-    }
-
     object_property_set_int(OBJECT(cpu), MPCORE_PERIPHBASE, "reset-cbar", &err);
-    if (err) {
-        error_report_err(err);
-        exit(1);
-    }
     object_property_set_bool(OBJECT(cpu), true, "realized", &err);
+
     if (err) {
         error_report_err(err);
         exit(1);
hw/arm/xlnx-ep108.c +1 -1
index f94da86..29948b2 100644
@@ -41,7 +41,7 @@ static void xlnx_ep108_init(MachineState *machine)
 
     object_property_set_bool(OBJECT(&s->soc), true, "realized", &err);
     if (err) {
-        error_report("%s", error_get_pretty(err));
+        error_report_err(err);
         exit(1);
     }
 
hw/arm/xlnx-zynqmp.c +1 -28
index 388baef..89624f9 100644
@@ -120,10 +120,6 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
     qdev_prop_set_uint32(DEVICE(&s->gic), "revision", 2);
     qdev_prop_set_uint32(DEVICE(&s->gic), "num-cpu", XLNX_ZYNQMP_NUM_APU_CPUS);
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
-    if (err) {
-        error_propagate((errp), (err));
-        return;
-    }
     assert(ARRAY_SIZE(xlnx_zynqmp_gic_regions) == XLNX_ZYNQMP_GIC_REGIONS);
     for (i = 0; i < XLNX_ZYNQMP_GIC_REGIONS; i++) {
         SysBusDevice *gic = SYS_BUS_DEVICE(&s->gic);
@@ -163,17 +159,9 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
 
         object_property_set_int(OBJECT(&s->apu_cpu[i]), GIC_BASE_ADDR,
                                 "reset-cbar", &err);
-        if (err) {
-            error_propagate((errp), (err));
-            return;
-        }
 
         object_property_set_bool(OBJECT(&s->apu_cpu[i]), true, "realized",
                                  &err);
-        if (err) {
-            error_propagate((errp), (err));
-            return;
-        }
 
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gic), i,
                            qdev_get_gpio_in(DEVICE(&s->apu_cpu[i]),
@@ -201,17 +189,9 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
 
         object_property_set_bool(OBJECT(&s->rpu_cpu[i]), true, "reset-hivecs",
                                  &err);
-        if (err != NULL) {
-            error_propagate(errp, err);
-            return;
-        }
 
         object_property_set_bool(OBJECT(&s->rpu_cpu[i]), true, "realized",
                                  &err);
-        if (err) {
-            error_propagate((errp), (err));
-            return;
-        }
     }
 
     if (!s->boot_cpu_ptr) {
@@ -231,10 +211,6 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
             qdev_set_nic_properties(DEVICE(&s->gem[i]), nd);
         }
         object_property_set_bool(OBJECT(&s->gem[i]), true, "realized", &err);
-        if (err) {
-            error_propagate((errp), (err));
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem[i]), 0, gem_addr[i]);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem[i]), 0,
                            gic_spi[gem_intr[i]]);
@@ -242,14 +218,11 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
 
     for (i = 0; i < XLNX_ZYNQMP_NUM_UARTS; i++) {
         object_property_set_bool(OBJECT(&s->uart[i]), true, "realized", &err);
-        if (err) {
-            error_propagate((errp), (err));
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, uart_addr[i]);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
                            gic_spi[uart_intr[i]]);
     }
+    error_propagate((errp), (err));
 }
 
 static Property xlnx_zynqmp_props[] = {
Signature · 1 line
1.9.1
#18 lore raw

RFC PATCH 11/25 arm: fsl-imx*: Update error API usages

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures.

Cc Jean-Christophe Dubois <[email protected]>
Cc Peter Maydell <[email protected]>

---

 hw/arm/fsl-imx25.c | 46 ++--------------------------------------------
 hw/arm/fsl-imx31.c | 42 ++----------------------------------------
 2 files changed, 4 insertions(+), 84 deletions(-)
hw/arm/fsl-imx25.c +2 -44
index 6d157c9..f510698 100644
@@ -72,16 +72,8 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
     Error *err = NULL;
 
     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
 
     object_property_set_bool(OBJECT(&s->avic), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->avic), 0, FSL_IMX25_AVIC_ADDR);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->avic), 0,
                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ));
@@ -89,10 +81,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                        qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ));
 
     object_property_set_bool(OBJECT(&s->ccm), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->ccm), 0, FSL_IMX25_CCM_ADDR);
 
     /* Initialize all UARTs */
@@ -123,10 +111,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
         }
 
         object_property_set_bool(OBJECT(&s->uart[i]), true, "realized", &err);
-        if (err) {
-            error_propagate(errp, err);
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->uart[i]), 0, serial_table[i].addr);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart[i]), 0,
                            qdev_get_gpio_in(DEVICE(&s->avic),
@@ -148,10 +132,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
         s->gpt[i].ccm = DEVICE(&s->ccm);
 
         object_property_set_bool(OBJECT(&s->gpt[i]), true, "realized", &err);
-        if (err) {
-            error_propagate(errp, err);
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpt[i]), 0, gpt_table[i].addr);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpt[i]), 0,
                            qdev_get_gpio_in(DEVICE(&s->avic),
@@ -171,10 +151,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
         s->epit[i].ccm = DEVICE(&s->ccm);
 
         object_property_set_bool(OBJECT(&s->epit[i]), true, "realized", &err);
-        if (err) {
-            error_propagate(errp, err);
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->epit[i]), 0, epit_table[i].addr);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->epit[i]), 0,
                            qdev_get_gpio_in(DEVICE(&s->avic),
@@ -183,10 +159,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
 
     qdev_set_nic_properties(DEVICE(&s->fec), &nd_table[0]);
     object_property_set_bool(OBJECT(&s->fec), true, "realized", &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->fec), 0, FSL_IMX25_FEC_ADDR);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->fec), 0,
                        qdev_get_gpio_in(DEVICE(&s->avic), FSL_IMX25_FEC_IRQ));
@@ -204,10 +176,6 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
         };
 
         object_property_set_bool(OBJECT(&s->i2c[i]), true, "realized", &err);
-        if (err) {
-            error_propagate(errp, err);
-            return;
-        }
         sysbus_mmio_map(SYS_BUS_DEVICE(&s->i2c[i]), 0, i2c_table[i].addr);
         sysbus_connect_irq(SYS_BUS_DEVICE(&s->i2c[i]), 0,
                            qdev_get_gpio_in(DEVICE(&s->avic),
@@ -217,28 +185,16 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
     /* initialize 2 x 16 KB ROM */
     memory_region_init_rom_device(&s->rom[0], NULL, NULL, NULL,
                                   "imx25.rom0", FSL_IMX25_ROM0_SIZE, &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     memory_region_add_subregion(get_system_memory(), FSL_IMX25_ROM0_ADDR,
                                 &s->rom[0]);
     memory_region_init_rom_device(&s->rom[1], NULL, NULL, NULL,
                                   "imx25.rom1", FSL_IMX25_ROM1_SIZE, &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     memory_region_add_subregion(get_system_memory(), FSL_IMX25_ROM1_ADDR,
                                 &s->rom[1]);
 
     /* initialize internal RAM (128 KB) */
     memory_region_init_ram(&s->iram, NULL, "imx25.iram", FSL_IMX25_IRAM_SIZE,
                            &err);
-    if (err) {
-        error_propagate(errp, err);
-        return;
-    }
     memory_region_add_subregion(get_system_memory(), FSL_IMX25_IRAM_ADDR,
                                 &s->iram);
     vmstate_register_ram_global(&s->iram);
@@ -248,6 +204,8 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                              &s->iram, 0, FSL_IMX25_IRAM_ALIAS_SIZE);
     memory_region_add_subregion(get_system_memory(), FSL_IMX25_IRAM_ALIAS_ADDR,
                                 &s->iram_alias);
+
+    error_propagate(errp, err);
 }
 
 static void fsl_imx25_class_init(ObjectClass *oc, void *data)
hw/arm/fsl-imx31.c +2 -40

104 lines not on this page · raw message

Signature · 1 line
1.9.1
#19 lore raw

RFC PATCH 12/25 arm: netduino: Update error API usages

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures. Use error_report_err as appropriate.

Cc Alistair Francis <[email protected]>
Cc Peter Maydell <[email protected]>

---

 hw/arm/netduino2.c     |  2 +-
 hw/arm/stm32f205_soc.c | 14 ++------------
 2 files changed, 3 insertions(+), 13 deletions(-)
hw/arm/netduino2.c +1 -1
index 8f26780..9a694bb 100644
@@ -38,7 +38,7 @@ static void netduino2_init(MachineState *machine)
     qdev_prop_set_string(dev, "cpu-model", "cortex-m3");
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
-        error_report("%s", error_get_pretty(err));
+        error_report_err(err);
         exit(1);
     }
 }
hw/arm/stm32f205_soc.c +2 -12
index 0f3bdc7..015deb2 100644
@@ -94,10 +94,6 @@ static void stm32f205_soc_realize(DeviceState *dev_soc, Error **errp)
     /* System configuration controller */
     syscfgdev = DEVICE(&s->syscfg);
     object_property_set_bool(OBJECT(&s->syscfg), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     syscfgbusdev = SYS_BUS_DEVICE(syscfgdev);
     sysbus_mmio_map(syscfgbusdev, 0, 0x40013800);
     sysbus_connect_irq(syscfgbusdev, 0, pic[71]);
@@ -106,10 +102,6 @@ static void stm32f205_soc_realize(DeviceState *dev_soc, Error **errp)
     for (i = 0; i < STM_NUM_USARTS; i++) {
         usartdev = DEVICE(&(s->usart[i]));
         object_property_set_bool(OBJECT(&s->usart[i]), true, "realized", &err);
-        if (err != NULL) {
-            error_propagate(errp, err);
-            return;
-        }
         usartbusdev = SYS_BUS_DEVICE(usartdev);
         sysbus_mmio_map(usartbusdev, 0, usart_addr[i]);
         sysbus_connect_irq(usartbusdev, 0, pic[usart_irq[i]]);
@@ -120,14 +112,12 @@ static void stm32f205_soc_realize(DeviceState *dev_soc, Error **errp)
         timerdev = DEVICE(&(s->timer[i]));
         qdev_prop_set_uint64(timerdev, "clock-frequency", 1000000000);
         object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
-        if (err != NULL) {
-            error_propagate(errp, err);
-            return;
-        }
         timerbusdev = SYS_BUS_DEVICE(timerdev);
         sysbus_mmio_map(timerbusdev, 0, timer_addr[i]);
         sysbus_connect_irq(timerbusdev, 0, pic[timer_irq[i]]);
     }
+
+    error_propagate(errp, err);
 }
 
 static Property stm32f205_soc_properties[] = {
Signature · 1 line
1.9.1
#20 lore raw

RFC PATCH 13/25 arm: allwinner: Update error API usages

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures. Prefix errors as appropriate and raise all errors at once at the end of machine init.

---

 hw/arm/allwinner-a10.c | 18 ++----------------
 hw/arm/cubieboard.c    | 28 ++++++++++------------------
 2 files changed, 12 insertions(+), 34 deletions(-)
hw/arm/allwinner-a10.c +2 -16
index ff249af..2da6cf9 100644
@@ -50,18 +50,10 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     Error *err = NULL;
 
     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     irq = qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_IRQ);
     fiq = qdev_get_gpio_in(DEVICE(&s->cpu), ARM_CPU_FIQ);
 
     object_property_set_bool(OBJECT(&s->intc), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbusdev = SYS_BUS_DEVICE(&s->intc);
     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIC_REG_BASE);
     sysbus_connect_irq(sysbusdev, 0, irq);
@@ -71,10 +63,6 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     }
 
     object_property_set_bool(OBJECT(&s->timer), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbusdev = SYS_BUS_DEVICE(&s->timer);
     sysbus_mmio_map(sysbusdev, 0, AW_A10_PIT_REG_BASE);
     sysbus_connect_irq(sysbusdev, 0, s->irq[22]);
@@ -85,10 +73,6 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     sysbus_connect_irq(sysbusdev, 5, s->irq[68]);
 
     object_property_set_bool(OBJECT(&s->emac), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbusdev = SYS_BUS_DEVICE(&s->emac);
     sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE);
     sysbus_connect_irq(sysbusdev, 0, s->irq[55]);
@@ -96,6 +80,8 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     /* FIXME use a qdev chardev prop instead of serial_hds[] */
     serial_mm_init(get_system_memory(), AW_A10_UART0_REG_BASE, 2, s->irq[1],
                    115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
+
+    error_propagate(errp, err);
 }
 
 static void aw_a10_class_init(ObjectClass *oc, void *data)
hw/arm/cubieboard.c +10 -18
index 1582250..91d9026 100644
@@ -37,29 +37,21 @@ static void cubieboard_init(MachineState *machine)
 
     s->a10 = AW_A10(object_new(TYPE_AW_A10));
 
-    object_property_set_int(OBJECT(&s->a10->emac), 1, "phy-addr", &err);
-    if (err != NULL) {
-        error_report("Couldn't set phy address: %s", error_get_pretty(err));
-        exit(1);
-    }
+    object_property_set_int(OBJECT(&s->a10->emac), 1, "ph-addr", &err);
+    error_prefix(err, "Couldn't set phy address: ");
 
-    object_property_set_int(OBJECT(&s->a10->timer), 32768, "clk0-freq", &err);
-    if (err != NULL) {
-        error_report("Couldn't set clk0 frequency: %s", error_get_pretty(err));
-        exit(1);
-    }
+    object_property_set_int(OBJECT(&s->a10->timer), 32768, "ck0-freq", &err);
+    error_prefix(err, "Couldn't set clk0 frequency: ");
 
-    object_property_set_int(OBJECT(&s->a10->timer), 24000000, "clk1-freq",
+    object_property_set_int(OBJECT(&s->a10->timer), 24000000, "ck1-freq",
                             &err);
-    if (err != NULL) {
-        error_report("Couldn't set clk1 frequency: %s", error_get_pretty(err));
-        exit(1);
-    }
+    error_prefix(err, "Couldn't set clk1 frequency:");
 
     object_property_set_bool(OBJECT(s->a10), true, "realized", &err);
-    if (err != NULL) {
-        error_report("Couldn't realize Allwinner A10: %s",
-                     error_get_pretty(err));
+    error_prefix(err, "Couldn't realize Allwinner A10: ");
+
+    if (err) {
+        error_report_err(err);
         exit(1);
     }
 
Signature · 1 line
1.9.1
#21 lore raw

RFC PATCH 14/25 arm: digic: Update error API usages

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures. Prefix errors and use error_report_err() as appropriate.

Cc Antony Pavlov <[email protected]>
Cc Peter Maydell <[email protected]>

---

 hw/arm/digic.c        | 19 ++-----------------
 hw/arm/digic_boards.c |  4 ++--
 2 files changed, 4 insertions(+), 19 deletions(-)
hw/arm/digic.c +2 -17
index ec8c330..2632517 100644
@@ -60,36 +60,21 @@ static void digic_realize(DeviceState *dev, Error **errp)
     int i;
 
     object_property_set_bool(OBJECT(&s->cpu), true, "reset-hivecs", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
-
     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     for (i = 0; i < DIGIC4_NB_TIMERS; i++) {
         object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
-        if (err != NULL) {
-            error_propagate(errp, err);
-            return;
-        }
 
         sbd = SYS_BUS_DEVICE(&s->timer[i]);
         sysbus_mmio_map(sbd, 0, DIGIC4_TIMER_BASE(i));
     }
 
     object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     sbd = SYS_BUS_DEVICE(&s->uart);
     sysbus_mmio_map(sbd, 0, DIGIC_UART_BASE);
+
+    error_propagate(errp, err);
 }
 
 static void digic_class_init(ObjectClass *oc, void *data)
hw/arm/digic_boards.c +2 -2
index f8ba9e5..4256ece 100644
@@ -64,8 +64,8 @@ static void digic4_board_init(DigicBoard *board)
     s->digic = DIGIC(object_new(TYPE_DIGIC));
     object_property_set_bool(OBJECT(s->digic), true, "realized", &err);
     if (err != NULL) {
-        error_report("Couldn't realize DIGIC SoC: %s",
-                     error_get_pretty(err));
+        error_prefix(err, "Couldn't realize DIGIC SoC: ");
+        error_report_err(err);
         exit(1);
     }
 
Signature · 1 line
1.9.1
#22 lore raw

RFC PATCH 15/25 cpu: arm: Remove un-needed error checking

In reply to Peter Crosthwaite

Collect all errors and remove constant checking for realize failures.

---

 hw/cpu/a15mpcore.c       |  6 ++----
 hw/cpu/a9mpcore.c        | 22 ++--------------------
 hw/cpu/arm11mpcore.c     | 18 ++----------------
 hw/cpu/realview_mpcore.c |  9 +--------
 4 files changed, 7 insertions(+), 48 deletions(-)
hw/cpu/a15mpcore.c +2 -4
index 58ac02e..b7da299 100644
@@ -57,10 +57,6 @@ static void a15mp_priv_realize(DeviceState *dev, Error **errp)
     qdev_prop_set_uint32(gicdev, "num-cpu", s->num_cpu);
     qdev_prop_set_uint32(gicdev, "num-irq", s->num_irq);
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     busdev = SYS_BUS_DEVICE(&s->gic);
 
     /* Pass through outbound IRQ lines from the GIC */
@@ -104,6 +100,8 @@ static void a15mp_priv_realize(DeviceState *dev, Error **errp)
                                 sysbus_mmio_get_region(busdev, 0));
     memory_region_add_subregion(&s->container, 0x2000,
                                 sysbus_mmio_get_region(busdev, 1));
+
+    error_propagate(errp, err);
 }
 
 static Property a15mp_priv_properties[] = {
hw/cpu/a9mpcore.c +2 -20
index c09358c..41f5a96 100644
@@ -53,20 +53,12 @@ static void a9mp_priv_realize(DeviceState *dev, Error **errp)
     scudev = DEVICE(&s->scu);
     qdev_prop_set_uint32(scudev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->scu), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     scubusdev = SYS_BUS_DEVICE(&s->scu);
 
     gicdev = DEVICE(&s->gic);
     qdev_prop_set_uint32(gicdev, "num-cpu", s->num_cpu);
     qdev_prop_set_uint32(gicdev, "num-irq", s->num_irq);
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     gicbusdev = SYS_BUS_DEVICE(&s->gic);
 
     /* Pass through outbound IRQ lines from the GIC */
@@ -78,28 +70,16 @@ static void a9mp_priv_realize(DeviceState *dev, Error **errp)
     gtimerdev = DEVICE(&s->gtimer);
     qdev_prop_set_uint32(gtimerdev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->gtimer), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     gtimerbusdev = SYS_BUS_DEVICE(&s->gtimer);
 
     mptimerdev = DEVICE(&s->mptimer);
     qdev_prop_set_uint32(mptimerdev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->mptimer), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     mptimerbusdev = SYS_BUS_DEVICE(&s->mptimer);
 
     wdtdev = DEVICE(&s->wdt);
     qdev_prop_set_uint32(wdtdev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->wdt), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     wdtbusdev = SYS_BUS_DEVICE(&s->wdt);
 
     /* Memory map (addresses are offsets from PERIPHBASE):
@@ -141,6 +121,8 @@ static void a9mp_priv_realize(DeviceState *dev, Error **errp)
         sysbus_connect_irq(wdtbusdev, i,
                            qdev_get_gpio_in(gicdev, ppibase + 30));
     }
+
+    error_propagate(errp, err);
 }
 
 static Property a9mp_priv_properties[] = {
hw/cpu/arm11mpcore.c +2 -16
index 717d3e4..5ba8977 100644
@@ -74,18 +74,10 @@ static void mpcore_priv_realize(DeviceState *dev, Error **errp)
 
     qdev_prop_set_uint32(scudev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->scu), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     qdev_prop_set_uint32(gicdev, "num-cpu", s->num_cpu);
     qdev_prop_set_uint32(gicdev, "num-irq", s->num_irq);
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     /* Pass through outbound IRQ lines from the GIC */
     sysbus_pass_irq(sbd, SYS_BUS_DEVICE(&s->gic));
@@ -95,19 +87,13 @@ static void mpcore_priv_realize(DeviceState *dev, Error **errp)
 
     qdev_prop_set_uint32(mptimerdev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->mptimer), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     qdev_prop_set_uint32(wdtimerdev, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->wdtimer), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
 
     mpcore_priv_map_setup(s);
+
+    error_propagate(errp, err);
 }
 
 static void mpcore_priv_initfn(Object *obj)
hw/cpu/realview_mpcore.c +1 -8
index c39a2da..f6c3b89 100644
@@ -66,10 +66,6 @@ static void realview_mpcore_realize(DeviceState *dev, Error **errp)
 
     qdev_prop_set_uint32(priv, "num-cpu", s->num_cpu);
     object_property_set_bool(OBJECT(&s->priv), true, "realized", &err);
-    if (err != NULL) {
-        error_propagate(errp, err);
-        return;
-    }
     sysbus_pass_irq(sbd, SYS_BUS_DEVICE(&s->priv));
     for (i = 0; i < 32; i++) {
         s->cpuic[i] = qdev_get_gpio_in(priv, i);
@@ -77,10 +73,6 @@ static void realview_mpcore_realize(DeviceState *dev, Error **errp)
     /* ??? IRQ routing is hardcoded to "normal" mode.  */
     for (n = 0; n < 4; n++) {
         object_property_set_bool(OBJECT(&s->gic[n]), true, "realized", &err);
-        if (err != NULL) {
-            error_propagate(errp, err);
-            return;
-        }
         gic = DEVICE(&s->gic[n]);
         gicbusdev = SYS_BUS_DEVICE(&s->gic[n]);
         sysbus_mmio_map(gicbusdev, 0, 0x10040000 + n * 0x10000);
@@ -90,6 +82,7 @@ static void realview_mpcore_realize(DeviceState *dev, Error **errp)
         }
     }
     qdev_init_gpio_in(dev, mpcore_rirq_set_irq, 64);
+    error_propagate(errp, err);
 }
 
 static void mpcore_rirq_init(Object *obj)
Signature · 1 line
1.9.1
#23 lore raw

RFC PATCH 16/25 ppc: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate.

---

 hw/ppc/e500.c      | 4 ++--
 hw/ppc/spapr.c     | 4 ++--
 hw/ppc/spapr_drc.c | 6 ++----
 3 files changed, 6 insertions(+), 8 deletions(-)
hw/ppc/e500.c +2 -2
index d300846..798e03d 100644
@@ -751,8 +751,8 @@ static qemu_irq *ppce500_init_mpic(MachineState *machine, PPCE500Params *params,
             dev = ppce500_init_mpic_kvm(params, irqs, &err);
         }
         if (machine_kernel_irqchip_required(machine) && !dev) {
-            error_report("kernel_irqchip requested but unavailable: %s",
-                         error_get_pretty(err));
+            error_prefix(err, "kernel_irqchip requested but unavailable: ");
+            error_report_err(err);
             exit(1);
         }
     }
hw/ppc/spapr.c +2 -2
index bf0c64f..0cbca9f 100644
@@ -121,8 +121,8 @@ static XICSState *xics_system_init(MachineState *machine,
             icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs, &err);
         }
         if (machine_kernel_irqchip_required(machine) && !icp) {
-            error_report("kernel_irqchip requested but unavailable: %s",
-                         error_get_pretty(err));
+            error_prefix(err, "kernel_irqchip requested but unavailable: ");
+            error_report_err(err);
         }
     }
 
hw/ppc/spapr_drc.c +2 -4
index ee87432..9837ef2 100644
@@ -418,8 +418,7 @@ static void realize(DeviceState *d, Error **errp)
     object_property_add_alias(root_container, link_name,
                               drc->owner, child_name, &err);
     if (err) {
-        error_report("%s", error_get_pretty(err));
-        error_free(err);
+        error_report_err(err);
         object_unref(OBJECT(drc));
     }
     g_free(child_name);
@@ -439,8 +438,7 @@ static void unrealize(DeviceState *d, Error **errp)
     snprintf(name, sizeof(name), "%x", drck->get_index(drc));
     object_property_del(root_container, name, &err);
     if (err) {
-        error_report("%s", error_get_pretty(err));
-        error_free(err);
+        error_report_err(err);
         object_unref(OBJECT(drc));
     }
 }
Signature · 1 line
1.9.1
#24 lore raw

RFC PATCH 17/25 i386: pc: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate.

---

 hw/i386/pc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/i386/pc.c +2 -3
index 9f2924e..1edee5b 100644
@@ -1276,9 +1276,8 @@ void pc_acpi_init(const char *default_dsdt)
 
         acpi_table_add_builtin(opts, &err);
         if (err) {
-            error_report("WARNING: failed to load %s: %s", filename,
-                         error_get_pretty(err));
-            error_free(err);
+            error_prefix(err, "WARNING: failed to load %s: ", filename);
+            error_report_err(err);
         }
         g_free(filename);
     }
Signature · 1 line
1.9.1
#25 lore raw

RFC PATCH 18/25 monitor: update error API usages

In reply to Peter Crosthwaite

Convert monitor_printf() to a error API printfer and use error_printf_fn().

---

 hmp.c                     | 33 +++++++++++++--------------------
 include/monitor/monitor.h |  2 +-
 monitor.c                 | 11 +++++------
 stubs/mon-printf.c        |  2 +-
 4 files changed, 20 insertions(+), 28 deletions(-)
hmp.c +13 -20
index 3f807b7..82e1309 100644
@@ -40,8 +40,7 @@ static void hmp_handle_error(Monitor *mon, Error **errp)
 {
     assert(errp);
     if (*errp) {
-        monitor_printf(mon, "%s\n", error_get_pretty(*errp));
-        error_free(*errp);
+        error_printf_fn(*errp, monitor_printf, mon);
     }
 }
 
@@ -534,8 +533,7 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
 
     info = qmp_query_vnc(&err);
     if (err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(err));
-        error_free(err);
+        error_printf_fn(err, monitor_printf, mon);
         return;
     }
 
@@ -657,8 +655,7 @@ void hmp_info_balloon(Monitor *mon, const QDict *qdict)
 
     info = qmp_query_balloon(&err);
     if (err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(err));
-        error_free(err);
+        error_printf_fn(err, monitor_printf, mon);
         return;
     }
 
@@ -926,8 +923,7 @@ void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
 
     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
     if (err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(err));
-        error_free(err);
+        error_printf_fn(err, monitor_printf, mon);
         return;
     }
 
@@ -1020,8 +1016,8 @@ void hmp_balloon(Monitor *mon, const QDict *qdict)
 
     qmp_balloon(value, &err);
     if (err) {
-        monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "balloon: ");
+        error_printf_fn(err, monitor_printf, mon);
     }
 }
 
@@ -1169,8 +1165,7 @@ void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
 
     qmp_migrate_set_cache_size(value, &err);
     if (err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(err));
-        error_free(err);
+        error_printf_fn(err, monitor_printf, mon);
         return;
     }
 }
@@ -1207,9 +1202,8 @@ void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
     qapi_free_MigrationCapabilityStatusList(caps);
 
     if (err) {
-        monitor_printf(mon, "migrate_set_capability: %s\n",
-                       error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "migrate_set_capability: ");
+        error_printf_fn(err, monitor_printf, mon);
     }
 }
 
@@ -1249,9 +1243,8 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
     }
 
     if (err) {
-        monitor_printf(mon, "migrate_set_parameter: %s\n",
-                       error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "migrate_set_parameter: ");
+        error_printf_fn(err, monitor_printf, mon);
     }
 }
 
@@ -1483,8 +1476,8 @@ void hmp_migrate(Monitor *mon, const QDict *qdict)
 
     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
     if (err) {
-        monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "migrate: ");
+        error_printf_fn(err, monitor_printf, mon);
         return;
     }
 
include/monitor/monitor.h +1 -1
index 9aff47e..2900a5c 100644
@@ -33,7 +33,7 @@ int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp);
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
-void monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
+void monitor_printf(void *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 void monitor_flush(Monitor *mon);
 int monitor_set_cpu(int cpu_index);
 int monitor_get_cpu_index(void);
monitor.c +5 -6
index 5455ab9..b0747f4 100644
@@ -363,11 +363,12 @@ void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
     g_free(buf);
 }
 
-void monitor_printf(Monitor *mon, const char *fmt, ...)
+void monitor_printf(void *mon, const char *fmt, ...)
 {
+    Monitor *monitor = mon;
     va_list ap;
     va_start(ap, fmt);
-    monitor_vprintf(mon, fmt, ap);
+    monitor_vprintf(monitor, fmt, ap);
     va_end(ap);
 }
 
@@ -1417,8 +1418,7 @@ static void hmp_boot_set(Monitor *mon, const QDict *qdict)
 
     qemu_boot_set(bootdevice, &local_err);
     if (local_err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(local_err));
-        error_free(local_err);
+        error_printf_fn(local_err, monitor_printf, mon);
     } else {
         monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
     }
@@ -5318,8 +5318,7 @@ static void bdrv_password_cb(void *opaque, const char *password,
 
     bdrv_add_key(bs, password, &local_err);
     if (local_err) {
-        monitor_printf(mon, "%s\n", error_get_pretty(local_err));
-        error_free(local_err);
+        error_printf_fn(local_err, monitor_printf, mon);
         ret = -EPERM;
     }
     if (mon->password_completion_cb)
stubs/mon-printf.c +1 -1
index 0ce2ca6..97a0c2a 100644
@@ -1,7 +1,7 @@
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 
-void monitor_printf(Monitor *mon, const char *fmt, ...)
+void monitor_printf(void *mon, const char *fmt, ...)
 {
 }
 
Signature · 1 line
1.9.1
#26 lore raw

RFC PATCH 19/25 qdev: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate.

---

 hw/core/qdev-properties.c | 7 +++----
 hw/core/qdev.c            | 6 +++---
 2 files changed, 6 insertions(+), 7 deletions(-)
hw/core/qdev-properties.c +3 -4
index 04fd80a..6494185 100644
@@ -1065,10 +1065,9 @@ static void qdev_prop_set_globals_for_type(DeviceState *dev,
         object_property_parse(OBJECT(dev), prop->value, prop->property, &err);
         if (err != NULL) {
             assert(prop->user_provided);
-            error_report("Warning: global %s.%s=%s ignored (%s)",
-                         prop->driver, prop->property, prop->value,
-                         error_get_pretty(err));
-            error_free(err);
+            error_prefix(err, "Warning: global %s.%s=%s ignored (%s)",
+                         prop->driver, prop->property, prop->value);
+            error_report_err(err);
             return;
         }
     }
hw/core/qdev.c +3 -3
index 52140ba..708f5fd 100644
@@ -365,9 +365,9 @@ void qdev_init_nofail(DeviceState *dev)
 
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err) {
-        error_report("Initialization of device %s failed: %s",
-                     object_get_typename(OBJECT(dev)),
-                     error_get_pretty(err));
+        error_prefix(err, "Initialization of device %s failed: ",
+                     object_get_typename(OBJECT(dev)));
+        error_report_err(err);
         exit(1);
     }
 }
Signature · 1 line
1.9.1
#27 lore raw

RFC PATCH 20/25 block: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate. Use the prefix + propagate combo rather than manual prefix + freeing.

---

 block.c                         |  5 ++---
 block/qcow2.c                   |  5 ++---
 block/qed.c                     |  5 ++---
 block/sheepdog.c                |  8 +++-----
 blockdev.c                      | 10 +++++-----
 hw/block/dataplane/virtio-blk.c |  5 ++---
 qemu-img.c                      | 38 ++++++++++++++++++--------------------
 7 files changed, 34 insertions(+), 42 deletions(-)
block.c +2 -3
index 090923c..f743f1e 100644
@@ -1233,9 +1233,8 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
         bdrv_unref(backing_hd);
         backing_hd = NULL;
         bs->open_flags |= BDRV_O_NO_BACKING;
-        error_setg(errp, "Could not open backing file: %s",
-                   error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Could not open backing file: ");
+        error_propagate(errp, local_err);
         goto free_exit;
     }
 
block/qcow2.c +2 -3
index ea34ae2..b5c45e1 100644
@@ -1581,9 +1581,8 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
     ret = qcow2_open(bs, options, flags, &local_err);
     QDECREF(options);
     if (local_err) {
-        error_setg(errp, "Could not reopen qcow2 layer: %s",
-                   error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Could not reopen qcow2 layer: ");
+        error_propagate(errp, local_err);
         return;
     } else if (ret < 0) {
         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
block/qed.c +2 -3
index 954ed00..0c4c5b6 100644
@@ -1605,9 +1605,8 @@ static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
     memset(s, 0, sizeof(BDRVQEDState));
     ret = bdrv_qed_open(bs, NULL, bs->open_flags, &local_err);
     if (local_err) {
-        error_setg(errp, "Could not reopen qed layer: %s",
-                   error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Could not reopen qed layer: ");
+        error_propagate(errp, local_err);
         return;
     } else if (ret < 0) {
         error_setg_errno(errp, -ret, "Could not reopen qed layer");
block/sheepdog.c +3 -5
index 9585beb..4712da0 100644
@@ -1776,8 +1776,7 @@ static int sd_create(const char *filename, QemuOpts *opts,
 
         fd = connect_to_sdog(s, &local_err);
         if (fd < 0) {
-            error_report("%s", error_get_pretty(local_err));
-            error_free(local_err);
+            error_report_err(local_err);
             ret = -EIO;
             goto out;
         }
@@ -2318,9 +2317,8 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
     ret = do_sd_create(s, &new_vid, 1, &local_err);
     if (ret < 0) {
-        error_report("failed to create inode for snapshot: %s",
-                     error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "failed to create inode for snapshot: ");
+        error_report_err(local_err);
         goto cleanup;
     }
 
blockdev.c +5 -5
index 6b48be6..78921fb 100644
@@ -1391,13 +1391,13 @@ static void internal_snapshot_abort(BlkTransactionState *common)
     }
 
     if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
-        error_report("Failed to delete snapshot with id '%s' and name '%s' on "
-                     "device '%s' in abort: %s",
+        error_prefix(local_error,
+                     "Failed to delete snapshot with id '%s' and name '%s' on "
+                     "device '%s' in abort:",
                      sn->id_str,
                      sn->name,
-                     bdrv_get_device_name(bs),
-                     error_get_pretty(local_error));
-        error_free(local_error);
+                     bdrv_get_device_name(bs));
+        error_report_err(local_error);
     }
 }
 
hw/block/dataplane/virtio-blk.c +2 -3
index 6106e46..ead5585 100644
@@ -166,9 +166,8 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
      */
     if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE,
                           &local_err)) {
-        error_setg(errp, "cannot start dataplane thread: %s",
-                   error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "cannot start dataplane thread: ");
+        error_propagate(errp, local_err);
         return;
     }
 
qemu-img.c +18 -20
index 6ff4e85..22fc1d0 100644
@@ -213,9 +213,8 @@ static BlockBackend *img_open(const char *id, const char *filename,
 
     blk = blk_new_open(id, filename, NULL, options, flags, &local_err);
     if (!blk) {
-        error_report("Could not open '%s': %s", filename,
-                     error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Could not open '%s': ", filename);
+        error_report_err(local_err);
         goto fail;
     }
 
@@ -359,8 +358,8 @@ static int img_create(int argc, char **argv)
     bdrv_img_create(filename, fmt, base_filename, base_fmt,
                     options, img_size, BDRV_O_FLAGS, &local_err, quiet);
     if (local_err) {
-        error_report("%s: %s", filename, error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "%s: ", filename);
+        error_report_err(local_err);
         goto fail;
     }
 
@@ -1712,9 +1711,8 @@ static int img_convert(int argc, char **argv)
         bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
     }
     if (local_err) {
-        error_report("Failed to load snapshot: %s",
-                     error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Failed to load snapshot: ");
+        error_report_err(local_err);
         ret = -1;
         goto out;
     }
@@ -1810,9 +1808,9 @@ static int img_convert(int argc, char **argv)
         /* Create the new image */
         ret = bdrv_create(drv, out_filename, opts, &local_err);
         if (ret < 0) {
-            error_report("%s: error while converting %s: %s",
-                         out_filename, out_fmt, error_get_pretty(local_err));
-            error_free(local_err);
+            error_prefix(local_err, "%s: error while converting %s: ",
+                         out_filename, out_fmt);
+            error_report_err(local_err);
             goto out;
         }
     }
@@ -2437,9 +2435,9 @@ static int img_snapshot(int argc, char **argv)
     case SNAPSHOT_DELETE:
         bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
         if (err) {
-            error_report("Could not delete snapshot '%s': (%s)",
-                         snapshot_name, error_get_pretty(err));
-            error_free(err);
+            error_prefix(err, "Could not delete snapshot '%s': ",
+                         snapshot_name);
+            error_report_err(err);
             ret = 1;
         }
         break;
@@ -2572,9 +2570,9 @@ static int img_rebase(int argc, char **argv)
         blk_old_backing = blk_new_open("old_backing", backing_name, NULL,
                                        options, src_flags, &local_err);
         if (!blk_old_backing) {
-            error_report("Could not open old backing file '%s': %s",
-                         backing_name, error_get_pretty(local_err));
-            error_free(local_err);
+            error_prefix(local_err, "Could not open old backing file '%s': ",
+                         backing_name);
+            error_report_err(local_err);
             goto out;
         }
 
@@ -2589,9 +2587,9 @@ static int img_rebase(int argc, char **argv)
             blk_new_backing = blk_new_open("new_backing", out_baseimg, NULL,
                                            options, src_flags, &local_err);
             if (!blk_new_backing) {
-                error_report("Could not open new backing file '%s': %s",
-                             out_baseimg, error_get_pretty(local_err));
-                error_free(local_err);
+                error_prefix(local_err, "Could not open new backing file '%s':",
+                             out_baseimg);
+                error_report_err(local_err);
                 goto out;
             }
         }
Signature · 1 line
1.9.1
#28 lore raw

RFC PATCH 21/25 tests: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate.

---

 tests/test-aio.c         | 5 ++---
 tests/test-thread-pool.c | 5 ++---
 tests/test-throttle.c    | 9 ++++-----
 3 files changed, 8 insertions(+), 11 deletions(-)
tests/test-aio.c +2 -3
index 217e337..daeeb47 100644
@@ -802,9 +802,8 @@ int main(int argc, char **argv)
 
     ctx = aio_context_new(&local_error);
     if (!ctx) {
-        error_report("Failed to create AIO Context: '%s'",
-                     error_get_pretty(local_error));
-        error_free(local_error);
+        error_prefix(local_err, "Failed to create AIO Context: ");
+        error_report_err(local_err);
         exit(1);
     }
     src = aio_get_g_source(ctx);
tests/test-thread-pool.c +2 -3
index 6a0b981..231c789 100644
@@ -229,9 +229,8 @@ int main(int argc, char **argv)
 
     ctx = aio_context_new(&local_error);
     if (!ctx) {
-        error_report("Failed to create AIO Context: '%s'",
-                     error_get_pretty(local_error));
-        error_free(local_error);
+        error_prefix(local_error, "Failed to create AIO Context:");
+        error_report_err(local_error);
         exit(1);
     }
     pool = aio_get_thread_pool(ctx);
tests/test-throttle.c +4 -5
index 85c9b6c..4996445 100644
@@ -587,12 +587,11 @@ int main(int argc, char **argv)
     ctx = qemu_get_aio_context();
 
     if (!ctx) {
-        error_report("Failed to create AIO Context: '%s'",
-                     local_error ? error_get_pretty(local_error) :
-                     "Failed to initialize the QEMU main loop");
-        if (local_error) {
-            error_free(local_error);
+        if (!local_error) {
+           error_setg(&local_error, "Failed to initialize the QEMU main loop");
         }
+        error_prefix(local_error, "Failed to create AIO Context: ");
+        error_report_err(local_error);
         exit(1);
     }
 
Signature · 1 line
1.9.1
#30 lore raw

RFC PATCH 23/25 scsi: Update error API usages

In reply to Peter Crosthwaite

Use the prefix + propagate combo rather than manual prefix + freeing.

---

 hw/scsi/vhost-scsi.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/scsi/vhost-scsi.c +2 -3
index 7eacca9..bd55ebc 100644
@@ -219,9 +219,8 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
     if (vs->conf.vhostfd) {
         vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, &err);
         if (vhostfd == -1) {
-            error_setg(errp, "vhost-scsi: unable to parse vhostfd: %s",
-                       error_get_pretty(err));
-            error_free(err);
+            error_prefix(err, "vhost-scsi: unable to parse vhostfd: ");
+            error_propagate(errp, err);
             return;
         }
     } else {
Signature · 1 line
1.9.1
#29 lore raw

RFC PATCH 22/25 usb: bus: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate. Use the prefix + propagate combo rather than manual prefix + freeing.

---

 hw/usb/bus.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
hw/usb/bus.c +4 -6
index 5f39e1e..44264ee 100644
@@ -329,9 +329,8 @@ static USBDevice *usb_try_create_simple(USBBus *bus, const char *name,
     }
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err) {
-        error_setg(errp, "Failed to initialize USB device '%s': %s",
-                   name, error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "Failed to initialize USB device '%s': ", name);
+        error_propagate(errp, err);
         object_unparent(OBJECT(dev));
         return NULL;
     }
@@ -722,9 +721,8 @@ USBDevice *usbdevice_create(const char *cmdline)
     }
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err) {
-        error_report("Failed to initialize USB device '%s': %s",
-                     f->name, error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "Failed to initialize USB device '%s': ", f->name);
+        error_report_err(err);
         object_unparent(OBJECT(dev));
         return NULL;
     }
Signature · 1 line
1.9.1
#31 lore raw

RFC PATCH 24/25 migration: savevm: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and convert monitor_printf to error_printf_fn.

---

 migration/savevm.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
migration/savevm.c +9 -13
index 6071215..683201d 100644
@@ -1266,12 +1266,10 @@ static int del_existing_snapshots(Monitor *mon, const char *name)
             bdrv_snapshot_find(bs, snapshot, name) >= 0) {
             bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
             if (err) {
-                monitor_printf(mon,
-                               "Error while deleting snapshot on device '%s':"
-                               " %s\n",
-                               bdrv_get_device_name(bs),
-                               error_get_pretty(err));
-                error_free(err);
+                error_prefix(err,
+                             "Error while deleting snapshot on device '%s':",
+                              bdrv_get_device_name(bs));
+                error_printf_fn(err, monitor_printf, mon);
                 return -1;
             }
         }
@@ -1360,7 +1358,7 @@ void hmp_savevm(Monitor *mon, const QDict *qdict)
     vm_state_size = qemu_ftell(f);
     qemu_fclose(f);
     if (ret < 0) {
-        monitor_printf(mon, "%s\n", error_get_pretty(local_err));
+        error_printf_fn(local_err, monitor_printf, mon);
         error_free(local_err);
         goto the_end;
     }
@@ -1512,12 +1510,10 @@ void hmp_delvm(Monitor *mon, const QDict *qdict)
             err = NULL;
             bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
             if (err) {
-                monitor_printf(mon,
-                               "Error while deleting snapshot on device '%s':"
-                               " %s\n",
-                               bdrv_get_device_name(bs),
-                               error_get_pretty(err));
-                error_free(err);
+                error_prefix(err,
+                             "Error while deleting snapshot on device '%s':",
+                             bdrv_get_device_name(bs));
+                error_printf_fn(err, monitor_printf, mon);
             }
         }
     }
Signature · 1 line
1.9.1
#32 lore raw

RFC PATCH 25/25 core: Update error API usages

In reply to Peter Crosthwaite

Use error_prefix() and error_report_err() as appropriate.

---

 arch_init.c | 5 ++---
 ui/vnc.c    | 5 ++---
 vl.c        | 7 +++----
 3 files changed, 7 insertions(+), 10 deletions(-)
arch_init.c +2 -3
index 38f5fb9..bb8db89 100644
@@ -258,9 +258,8 @@ void do_acpitable_option(const QemuOpts *opts)
 
     acpi_table_add(opts, &err);
     if (err) {
-        error_report("Wrong acpi table provided: %s",
-                     error_get_pretty(err));
-        error_free(err);
+        error_prefix(err, "Wrong acpi table provided: ");
+        error_report_err(err);
         exit(1);
     }
 #endif
ui/vnc.c +2 -3
index caf82f5..84324bf 100644
@@ -3775,9 +3775,8 @@ int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp)
     vnc_display_init(id);
     vnc_display_open(id, &local_err);
     if (local_err != NULL) {
-        error_report("Failed to start VNC server: %s",
-                     error_get_pretty(local_err));
-        error_free(local_err);
+        error_prefix(local_err, "Failed to start VNC server: ");
+        error_report_err(local_err);
         exit(1);
     }
     return 0;
vl.c +3 -4
index 584ca88..17dcffd 100644
@@ -4543,7 +4543,7 @@ int main(int argc, char **argv, char **envp)
         Error *local_err = NULL;
         qemu_boot_set(boot_once, &local_err);
         if (local_err) {
-            error_report("%s", error_get_pretty(local_err));
+            error_report_err(local_err);
             exit(1);
         }
         qemu_register_reset(restore_boot_order, g_strdup(boot_order));
@@ -4633,9 +4633,8 @@ int main(int argc, char **argv, char **envp)
         Error *local_err = NULL;
         qemu_start_incoming_migration(incoming, &local_err);
         if (local_err) {
-            error_report("-incoming %s: %s", incoming,
-                         error_get_pretty(local_err));
-            error_free(local_err);
+            error_prefix(local_err, "-incoming %s: ", incoming);
+            error_report_err(local_err);
             exit(1);
         }
     } else if (autostart) {
Signature · 1 line
1.9.1
#33 lore raw
In reply to Peter Crosthwaite

Quick initial high-level feedback, since I'm afraid real review will take a while (series is long, and I'm still swamped).

Quoted 23 lines · Peter Crosthwaite

Hi Markus and all,

This patch series adds support for automatically concatenating multiple errors to the one Error *.

I'll start with what I am actually trying to do, which is get rid of the boilerplate:

some_api_call(... , &local_err);

if (local_err) {
    error_propagate(errp, local_err);
    return;
}
some_similar_api_call(... , &local_err);
if (local_err) {
    error_propagate(errp, local_err);
    return;
}
some_similar_api_call(... , &local_err);
if (local_err) {
    error_propagate(errp, local_err);
    return;
}
Quoted 6 lines

...

It is usually 1 LOC for the API and 4 LOC for the error handling boiler plate making our code hard reading. This is particularly bad in hw/arm where we have a good number of fully QOMified SoCs and machine models, each of which need these checks on recursive realize calls and friends.

The problem is real. A way to avoid or even reduce the boiler plate would be nice, provided it's reasonably hard to misuse, and doesn't mess with the current usage patterns of the Error interface.

Quoted 7 lines

The removed LOC just in ARM pays for the extra core code needed to implement this. And the number of ARM machines is only going to grow.

So the plan is:

1: Allow an Error * to contain more that one actual error from API calls.

Can you explain *why* you want to support multiple errors?

Quoted 2 lines

2: Refactor key APIs (some_similar_api_call() in the above example) to not fatal when previous errors have occured in dependencies.

I'm afraid this is going to be non-trivial :)

For every place that now continues after errors, you need to show that continuing is safe.

Quoted 25 lines

Point 1 kind of got big on me. Patch 4 is the main event, listifying errors. The follow up issue however, is it tricky to get a sane definition of error_get_pretty for a multi-error. So instead the strategy is to remove error_get_pretty() and replace with some error API helper with well defined behaviour for multi-error. The two leading uses of error get pretty are prefixing an error, and reporting an error via a custom printf handler. So two new APIs are added for that (P5-6). There aren't many error_get_pretty's left after that, and they shouldn't be in the path of any multi-errors.

I think the error_prefix is valuable it its own right, as it now means the code for report or propagating a prefixed error is now consistent with the non-prefixed variants.

That is, we used to have:

/* If we are prefixing we have to do it this way: */ error_setg(errp, "some prefix %s", error_get_pretty(local_err)); error_free(local_err);

vs:

/* but if not prefixing it is like this: */
error_propagate(errp, local_err);

Quoted 6 lines

Now with this patch series the two are much more recognisable as the same with:

/* This code is almost the same as the above non-prefixed propagation */ error_prefix(local_err, "some prefix"):
error_propagate(errp, local_err);

Less flexible, but that might be a good thing. Need to see the actual uses to tell.

Quoted 11 lines

Point 2 is less about error API and more about machine generation. Sysbus, Memory and Qdev all have APIs that depend on successful device- init and realize calls for argument devices. As we are trying to remove the error detection for those argument devs, those APIs need to tweaked to handle realize failure. This actually wasn't as bad as I thought it would be. See patches (7-9).

All patches after that walk the various major subsystems converting error APIs to this new scheme and deleting now-unneeded boiler plate. ARM is first (P10-15) seeing good clean up of propagate handers.

Quoted 5 lines

So the net result for these ARM machines, is error behaviour that is something like a compiler. If any one thing fails, then machine-init (compilation) fails. But an early fail does not stop machine-init (compilation), instead it proceeds to the end collecting subsequent errors as it goes.

Simple compilers stop on first error. Not as bad as it may sound when your machine gets from running "make" to compiler dying on the first error real fast.

More ambitious compilers continue to diagnose more errors. This isn't trivial. The compiler has to satisfy post conditions even after an error, typically by synthesizing suitable error values. It has to take pains to avoid error cascades. Experienced users recognize when that effort fails, and typically ignore the remaining errors wholesale then.

In QEMU, error cascades might be less of a problem than with compilers. To tell for sure, we'd have to try.

However, satisfying post conditions is at least as much of a problem. More so since they're generally unstated. Can you explain your strategy for solving this one?

Quoted 11 lines

Some other interesting food for thought is the qemu_fdt APIs which I have been wanting to convert to error API but the local_err propagation is going to be brutal in heavy users like e500.c. This would solve that as fdt API could be easily made multi-error safe and clients like e500 can just collect multi-errors and single-fail at the end.

Long term, we can use this to catch cases of multiple genuine machine init errors in the one boot but that is a secondary goal to simply cutting down on code boilerplate. The best feature of this series is the diffstat.

Quoted 4 lines

Patches 1-3 are cleanup that can be taken independent of the series.

I think P3 may be obsolete from a recent merge, but i'll wait for architectural feedback before rebasing.

Sorry, only very high level feedback so far. Best I can do right now. Hope it's useful anyway.

#35 lore raw
In reply to Peter Crosthwaite
Quoted 4 lines · Peter Crosthwaite

Hi Markus and all,

This patch series adds support for automatically concatenating multiple errors to the one Error *.

Sounds interesting!

Quoted 4 lines

So the plan is:

1: Allow an Error * to contain more that one actual error from API calls.

Is this for all errors, or do you have to make a special call to make it obvious that a particular error can be concatenated to while the default is still to report programming error if a second error is attempted atop a regular error that hasn't requested concatenation?

Quoted 28 lines

2: Refactor key APIs (some_similar_api_call() in the above example) to not fatal when previous errors have occured in dependencies.

Point 1 kind of got big on me. Patch 4 is the main event, listifying errors. The follow up issue however, is it tricky to get a sane definition of error_get_pretty for a multi-error. So instead the strategy is to remove error_get_pretty() and replace with some error API helper with well defined behaviour for multi-error. The two leading uses of error get pretty are prefixing an error, and reporting an error via a custom printf handler. So two new APIs are added for that (P5-6). There aren't many error_get_pretty's left after that, and they shouldn't be in the path of any multi-errors.

I think the error_prefix is valuable it its own right, as it now means the code for report or propagating a prefixed error is now consistent with the non-prefixed variants.

That is, we used to have:

/* If we are prefixing we have to do it this way: */ error_setg(errp, "some prefix %s", error_get_pretty(local_err)); error_free(local_err);

vs:

/* but if not prefixing it is like this: */
error_propagate(errp, local_err);

Quoted 6 lines

Now with this patch series the two are much more recognisable as the same with:

/* This code is almost the same as the above non-prefixed propagation */ error_prefix(local_err, "some prefix"):
error_propagate(errp, local_err);

Seems nice in its own right.

Quoted 12 lines

Point 2 is less about error API and more about machine generation. Sysbus, Memory and Qdev all have APIs that depend on successful device- init and realize calls for argument devices. As we are trying to remove the error detection for those argument devs, those APIs need to tweaked to handle realize failure. This actually wasn't as bad as I thought it would be. See patches (7-9).

All patches after that walk the various major subsystems converting error APIs to this new scheme and deleting now-unneeded boiler plate. ARM is first (P10-15) seeing good clean up of propagate handers.

Quoted 5 lines

So the net result for these ARM machines, is error behaviour that is something like a compiler. If any one thing fails, then machine-init (compilation) fails. But an early fail does not stop machine-init (compilation), instead it proceeds to the end collecting subsequent errors as it goes.

Sometimes that causes more problems (ignoring an error and proceeding on can cause confusing followup errors), but usually it manages to work out.

Quoted 12 lines

Some other interesting food for thought is the qemu_fdt APIs which I have been wanting to convert to error API but the local_err propagation is going to be brutal in heavy users like e500.c. This would solve that as fdt API could be easily made multi-error safe and clients like e500 can just collect multi-errors and single-fail at the end.

Long term, we can use this to catch cases of multiple genuine machine init errors in the one boot but that is a secondary goal to simply cutting down on code boilerplate. The best feature of this series is the diffstat.

Quoted 4 lines

Patches 1-3 are cleanup that can be taken independent of the series.

I think P3 may be obsolete from a recent merge, but i'll wait for architectural feedback before rebasing.

Yeah, both Markus and I have been touching error.c lately, so a rebase will probably be needed.

__HAS_COVER__

Quoted 15 lines

Regards,
Peter
--END---

---
 __HAS_COVER__ | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 __HAS_COVER__
__HAS_COVER__ new file +0 -0
new file mode 100644
index 0000000..e69de29

Huh - whatever you are using to version your cover letter made the real diffstat be part of the signature rather than the main body of the email.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/25
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v1 01/25] exec: convert error_report to error_report_err

In reply to Peter Crosthwaite
Quoted 9 lines · Peter Crosthwaite

This is a standard error_report_err. Convert it.

---

 exec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 2/25
#4 lore raw

Re: [Qemu-devel] [RFC PATCH v1 02/25] s390x: virtio-ccw: Remove un-needed if guard

In reply to Peter Crosthwaite
Quoted 11 lines · Peter Crosthwaite

error_propagate is already NULL safe. Remove these un-needed if guards.

Cc Richard Henderson <[email protected]>
Cc Alexander Graf <[email protected]>

---

 hw/s390x/virtio-ccw.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)

We could probably do a Coccinelle patch to catch more offenders.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw

Re: [Qemu-devel] [RFC PATCH v1 03/25] error: Factor out common error setter logic

In reply to Peter Crosthwaite
Quoted 6 lines · Peter Crosthwaite

Currently set_error, set_error_errno, set_error_win32 do pretty much the same thing with only a little bit of string manipulation in the error code cases. Coreify that string manipulation as a callback and factor out the bulk of the error_set fns to a common helper that calls the callback at the right time.

Commit 5523750 pretty much already does this. This patch can be dropped when rebasing the series to latest.

Quoted 6 lines

---

 util/error.c | 81 +++++++++++++++++++++++++-----------------------------------
 1 file changed, 33 insertions(+), 48 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

Re: [Qemu-devel] [RFC PATCH v1 04/25] error: Add support for multiple errors

In reply to Peter Crosthwaite
Quoted 5 lines · Peter Crosthwaite

Allow errors to stack. If an error is already set, don't assert, instead, form a linked list. Recent errors are at the front of the list, older ones at the back.

The assertion against the destination erro already being set is

s/erro/error/

Quoted 1 line

removed.

Do we want to do that blindly, or do we want a design where users must explicitly ask for nested errors?

I'm wondering aloud here: (haven't actually thought too hard, but typing as I go)

Since your goal was reducing boilerplate, is there some way we could have:

myfunc1(..., error_add(errp));
myfunc2(..., error_add(errp));

be some way to mark errp as allowing error concatenation? That is, error_add() would return errp; if *errp was NULL it does nothing further, but *errp is non-NULL it then sets a flag in errp that it is okay for further errors to be concatenated. Then when setting or propagating an error, we can use the flag within errp to determine if the caller is okay with us appending to the existing error, or whether there may be a programming error in that we are detecting a followup error to an errp that wasn't properly cleared earlier.

Or maybe:

error_start_chaining(errp);
myfunc1(..., errp);
myfunc2(..., errp);
error_stop_chaining(errp);

where we use a counter of how many requests (since myfunc1() may itself call nested start/stop, so chaining is okay if the counter is non-zero).

Quoted 17 lines

copy/free are all to call their functionality recursively.

Propagation is implemented as concatenation of two error lists.

---

 qapi/common.json |  5 ++++-
 util/error.c     | 27 +++++++++++++++++++++------
 2 files changed, 25 insertions(+), 7 deletions(-)
qapi/common.json +0 -0
index bad56bf..04175db 100644

qapi/common.json line 25

Quoted 4 lines
qapi/common.json +1 -0
@@ -22,11 +22,14 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
+# @MultipleErrors: Multiple errors have occured

s/occured/occurred/

Missing a (since 2.5) designation.

Do we want to change the QMP wire format when multiple errors have been chained together, to return a linked list or array of those errors, for easier machine parsing of the individual errors? If so, it requires some documentation updates. If not, packing the chained error information into a single string is okay for humans, but not as nice for computers.

Quoted 16 lines
qapi/common.json +3 -1
+#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
+            'MultipleErrors' ] }
 
 ##
 # @VersionTriple
util/error.c +0 -0
index b93e5c8..890ce58 100644

util/error.c struct Error · line 22

Quoted 5 lines
util/error.c +1 -0
@@ -18,28 +18,25 @@ struct Error
 {
     char *msg;
     ErrorClass err_class;
+    struct Error *next;
 };

Merge conflicts in this area; but doesn't hold up review of the concept.

Quoted 6 lines
util/error.c +0 -1
 Error *error_abort;
 
 static void do_error_set(Error **errp, ErrorClass err_class,
                          void (*mod)(Error *, void *), void *mod_opaque,
-                         const char *fmt, ...)

util/error.c struct Error · line 36

Quoted 10 lines
util/error.c +1 -2
+                         const char *fmt, va_list ap)
 {
     Error *err;
-    va_list ap;
     int saved_errno = errno;
 
     if (errp == NULL) {
         return;
     }
-    assert(*errp == NULL);

Here's where I'm wondering if we can have some sort of flag to say whether the caller was okay with error concatentation, in which case this would be something like:

assert(!*errp || errp->chaining_okay);

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#34 lore raw
In reply to Markus Armbruster
Quoted 21 lines · Markus Armbruster

Quick initial high-level feedback, since I'm afraid real review will take a while (series is long, and I'm still swamped).

Quoted 6 lines

So the net result for these ARM machines, is error behaviour that is something like a compiler. If any one thing fails, then machine-init (compilation) fails. But an early fail does not stop machine-init (compilation), instead it proceeds to the end collecting subsequent errors as it goes.

Simple compilers stop on first error. Not as bad as it may sound when your machine gets from running "make" to compiler dying on the first error real fast.

More ambitious compilers continue to diagnose more errors. This isn't trivial. The compiler has to satisfy post conditions even after an error, typically by synthesizing suitable error values. It has to take pains to avoid error cascades. Experienced users recognize when that effort fails, and typically ignore the remaining errors wholesale then.

Quoted 6 lines

In QEMU, error cascades might be less of a problem than with compilers. To tell for sure, we'd have to try.

However, satisfying post conditions is at least as much of a problem. More so since they're generally unstated. Can you explain your strategy for solving this one?

And that's why I'm arguing that error chaining has to be an explicit opt-in, and not the default. Code that has been audited to be safe against cascading errors can use error chaining to reduce boilerplate, but the default behavior should continue to be treating the first error as fatal rather than getting the system into a worse state due to failed postconditions.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw

Re: [Qemu-devel] [RFC PATCH v1 05/25] error: Add error prefix API

In reply to Peter Crosthwaite
Quoted 5 lines · Peter Crosthwaite

Add an API to prefix an already set error with a caller-centric message.

If multiple errors are set, all are prefixed individually.

Might be nice to rebase your series to add this first, prior to multi-error support. (That is, while prefixing multiple errors requires multi-error support, adding caller-centric prefix might be useful even now without multi-error).

Quoted 12 lines

---

 include/qapi/error.h |  6 ++++++
 util/error.c         | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/qapi/error.h +0 -0
index f44c451..b25c72f 100644

include/qapi/error.h ErrorClass error_get_class(const Error *err); · line 84

Quoted 7 lines
include/qapi/error.h +4 -0
@@ -78,6 +78,12 @@ ErrorClass error_get_class(const Error *err);
 Error *error_copy(const Error *err);
 
 /**
+ * Prefix an error message with a formatted string.
+ */
+
+void error_prefix(Error *err, const char *fmt, ...);

The string is basically only adding details for human consumption.

include/qapi/error.h struct Error · line 23

Quoted 7 lines
include/qapi/error.h +2 -0
+++ b/util/error.c
@@ -19,6 +19,7 @@ struct Error
     char *msg;
     ErrorClass err_class;
     struct Error *next;
+    bool prefixed;
 };

I'm not sure I follow why this field is needed.

include/qapi/error.h const char *error_get_pretty(Error *err) · line 146

Quoted 7 lines
include/qapi/error.h +1 -0
 Error *error_abort;
@@ -142,6 +143,26 @@ const char *error_get_pretty(Error *err)
     return err->msg;
 }
 
+void error_prefix(Error *err, const char *fmt, ...) {

Brace on its own line.

Quoted 4 lines
include/qapi/error.h +4 -0
+    char *msg;
+    char *fmt_full;
+    va_list ap;
+

include/qapi/error.h const char *error_get_pretty(Error *err) · line 160

Quoted 10 lines
include/qapi/error.h +10 -0
+    if (!err || err->prefixed) {
+        return;
+    }
+    err->prefixed = true;
+
+    msg = err->msg;
+    fmt_full =  g_strdup_printf("%s%%s", fmt);
+
+    va_start(ap, fmt);
+    err->msg = g_strdup_printf(fmt_full, ap, msg);

I don't think that is portable. Passing va_list as an argument to plain printf just isn't going to do what you think.
(There has been a request to add recursive printf via %r,
http://austingroupbugs.net/view.php?id=800, but glibc does not support the extension yet)

It's not to say that you can't chain, just that you can't chain like this. I don't know if using GString internally to error would make the matter any easier (but one of the patches that will probably land before yours, and thus affect your need to rebase, is mine which adds use of GString for adding a human-readable SUFFIX rather than prefix:
https://lists.gnu.org/archive/html/qemu-devel/2015-09/msg02818.html)

Quoted 9 lines
include/qapi/error.h +5 -0
+    va_end(ap);
+    g_free(fmt_full);
+    g_free(msg);
+}
+
 void error_report_err(Error *err)
 {
     error_report("%s", error_get_pretty(err));

include/qapi/error.h void error_propagate(Error **dst_errp, Error *local_err) · line 198

Quoted 8 lines
include/qapi/error.h +5 -0
@@ -170,6 +191,11 @@ void error_propagate(Error **dst_errp, Error *local_err)
 
         *dst_errp = local_err;
         for (i = local_err; i; i = i->next) {
+            /* Propagation implies that the caller is no longer the owner of the
+             * error. Therefore reset prefixes, so higher level handlers can
+             * prefix again.
+             */
+            i->prefixed = false;

I'm still not quite sure I buy the semantics of this flag.

Quoted 4 lines
include/qapi/error.h +0 -0
             dst_errp = &i->next;
         }
         *dst_errp = old_dst_err;
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#13 lore raw

Re: [Qemu-devel] [RFC PATCH v1 06/25] error: Add error_printf_fn()

In reply to Peter Crosthwaite
Quoted 9 lines · Peter Crosthwaite

Add an API to report an error with a custom printf function. Use this for the implementation of error_report_err().

---

 include/qapi/error.h |  7 +++++++
 util/error.c         | 22 ++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

Sounds independently useful.

include/qapi/error.h const char *error_get_pretty(Error *err); · line 97

Quoted 10 lines
include/qapi/error.h +1 -0
index b25c72f..0e5c311 100644
@@ -94,6 +94,13 @@ const char *error_get_pretty(Error *err);
 void error_report_err(Error *);
 
 /**
+ * Report an and free an error object using a custom printf implementation.

s/an and/and/

Quoted 12 lines
include/qapi/error.h +6 -0
+ */
+
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque);
+
+/**
  * Propagate an error to an indirect pointer to an error.  This function will
  * always transfer ownership of the error reference and handles the case where
  * dst_err is NULL correctly.  Errors after the first are discarded.
util/error.c +0 -1
index e9c23ce..c4656af 100644
--- a/util/error.c

util/error.c void error_free(Error *err) · line 199

Quoted 10 lines
util/error.c +9 -0
 
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque)
+{
+    if (err->next) {
+        error_printf_fn(err->next, printf_fn, printf_opaque);
+    }
+    printf_fn(printf_opaque, "%s\n", error_get_pretty(err));
+    error_free(err);
+}

Of course, if you rebase this to come independent of multi-error, it won't need the first 'if'. But it looks like a reasonable factorization.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 2/25
#5 lore raw

Re: [Qemu-devel] [RFC PATCH v1 02/25] s390x: virtio-ccw: Remove un-needed if guard

In reply to Peter Crosthwaite
Quoted 10 lines · On Thu, 10 Sep 2015 22:33:12 -0700 Peter Crosthwaite…

error_propagate is already NULL safe. Remove these un-needed if guards.

Cc Richard Henderson <[email protected]>
Cc Alexander Graf <[email protected]>

---

 hw/s390x/virtio-ccw.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)

Shorter code is good :)