Skip to contentSkip to conversation

: QError v2

RFC In review Luiz Capitulino 5 participants 17 messages Nov 4, 2009 – Nov 18, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover : QError v2

Hi,

I can't remember seeing updated versions of a RFC series, but this should
prevent Anthony's scripts from merging these patches.

This new QError version has two major changes: the static error table has
been dropped and I'm using symbolic names instead of error codes.

Now, a call to:

monitor_printf(mon, "husb: host usb device %d.%d is already open\n",

               bus_num, addr);

Would become something like:

qemu_error_new('DeviceAlreadyOpen', "{ 'bus_num': %d, 'addr': %d }",

               bus_num, addr);

Which is basically what Anthony and other people were asking for, the
only difference is that I'm not passing the symbolic name through the dictionary. The reason is that I have the impression it's less general (as it becomes mandatory to have a dict) and slightly more complicaded.

The symbolic name can be freely defined, but we can have the common ones
in qerror.h.

Hopefully this version addresses the most important issues.

Luiz.

#1 lore raw

PATCH 1/8 QJSon: Introduce qobject_from_json_va()

Simple wrapper to parse_json() that accepts a va_list, will be used by QError.

---
 qjson.c |    5 +++++
 qjson.h |    2 ++
 2 files changed, 7 insertions(+), 0 deletions(-)
qjson.c +5 -0
index 5f92996..02fcd83 100644
@@ -723,3 +723,8 @@ QObject *qobject_from_jsonf(const char *string, size_t *length, ...)
 
     return obj;
 }
+
+QObject *qobject_from_json_va(const char *string, va_list *ap)
+{
+    return parse_json(string, NULL, ap);
+}
qjson.h +2 -0
index 0c94954..da0d653 100644
@@ -14,10 +14,12 @@
 #ifndef QJSON_H
 #define QJSON_H
 
+#include <stdarg.h>
 #include "qobject.h"
 
 QObject *qobject_from_json(const char *string, size_t *length);
 QObject *qobject_from_jsonf(const char *string, size_t *length, ...)
     __attribute__((__format__ (__printf__, 1, 3)));
+QObject *qobject_from_json_va(const char *string, va_list *ap);
 
 #endif /* QJSON_H */
Signature · 1 line
1.6.5.2.143.g8cc62
#2 lore raw

PATCH 2/8 QString: Introduce qstring_append_chr()

In reply to Luiz Capitulino

It appends a C char to a QString.

---
 qstring.c |   24 +++++++++++++++++++-----
 qstring.h |    1 +
 2 files changed, 20 insertions(+), 5 deletions(-)
qstring.c +19 -5
index 441a9e6..e422bd9 100644
@@ -53,25 +53,39 @@ QString *qstring_from_str(const char *str)
     return qstring;
 }
 
-/* qstring_append(): Append a C string to a QString
- */
-void qstring_append(QString *qstring, const char *str)
+static void capacity_increase(QString *qstring, size_t len)
 {
-    size_t len = strlen(str);
-
     if (qstring->capacity < (qstring->length + len)) {
         qstring->capacity += len;
         qstring->capacity *= 2; /* use exponential growth */
 
         qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
     }
+}
+
+/* qstring_append(): Append a C string to a QString
+ */
+void qstring_append(QString *qstring, const char *str)
+{
+    size_t len = strlen(str);
 
+    capacity_increase(qstring, len);
     memcpy(qstring->string + qstring->length, str, len);
     qstring->length += len;
     qstring->string[qstring->length] = 0;
 }
 
 /**
+ * qstring_append_chr(): Append a C char to a QString
+ */
+void qstring_append_chr(QString *qstring, int c)
+{
+    capacity_increase(qstring, 1);
+    qstring->string[qstring->length++] = c;
+    qstring->string[qstring->length] = 0;
+}
+
+/**
  * qobject_to_qstring(): Convert a QObject to a QString
  */
 QString *qobject_to_qstring(const QObject *obj)
qstring.h +1 -0
index 65905d4..43581de 100644
@@ -14,6 +14,7 @@ QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 const char *qstring_get_str(const QString *qstring);
 void qstring_append(QString *qstring, const char *str);
+void qstring_append_chr(QString *qstring, int c);
 QString *qobject_to_qstring(const QObject *obj);
 
 #endif /* QSTRING_H */
Signature · 1 line
1.6.5.2.143.g8cc62
#3 lore raw

PATCH 3/8 Add qstring_append_chr() unit-test

In reply to Luiz Capitulino
---
 check-qstring.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
check-qstring.c +17 -0
index ea4dfd0..412038a 100644
@@ -55,6 +55,22 @@ START_TEST(qstring_get_str_test)
 }
 END_TEST
 
+START_TEST(qstring_append_chr_test)
+{
+    int i;
+    QString *qstring;
+    const char *str = "qstring append char unit-test";
+
+    qstring = qstring_new();
+
+    for (i = 0; str[i]; i++)
+        qstring_append_chr(qstring, str[i]);
+
+    fail_unless(strcmp(str, qstring_get_str(qstring)) == 0);
+    QDECREF(qstring);
+}
+END_TEST
+
 START_TEST(qobject_to_qstring_test)
 {
     QString *qstring;
@@ -78,6 +94,7 @@ static Suite *qstring_suite(void)
     tcase_add_test(qstring_public_tcase, qstring_from_str_test);
     tcase_add_test(qstring_public_tcase, qstring_destroy_test);
     tcase_add_test(qstring_public_tcase, qstring_get_str_test);
+    tcase_add_test(qstring_public_tcase, qstring_append_chr_test);
     tcase_add_test(qstring_public_tcase, qobject_to_qstring_test);
 
     return s;
Signature · 1 line
1.6.5.2.143.g8cc62
#4 lore raw

PATCH 4/8 QString: Introduce qstring_append_int()

In reply to Luiz Capitulino
---
 qstring.c |    8 ++++++++
 qstring.h |    2 ++
 2 files changed, 10 insertions(+), 0 deletions(-)
qstring.c +8 -0
index e422bd9..ad17769 100644
@@ -75,6 +75,14 @@ void qstring_append(QString *qstring, const char *str)
     qstring->string[qstring->length] = 0;
 }
 
+void qstring_append_int(QString *qstring, int64_t value)
+{
+    char num[32];
+
+    snprintf(num, sizeof(num), "%" PRId64, value);
+    qstring_append(qstring, num);
+}
+
 /**
  * qstring_append_chr(): Append a C char to a QString
  */
qstring.h +2 -0
index 43581de..c065331 100644
@@ -1,6 +1,7 @@
 #ifndef QSTRING_H
 #define QSTRING_H
 
+#include <stdint.h>
 #include "qobject.h"
 
 typedef struct QString {
@@ -13,6 +14,7 @@ typedef struct QString {
 QString *qstring_new(void);
 QString *qstring_from_str(const char *str);
 const char *qstring_get_str(const QString *qstring);
+void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
 QString *qobject_to_qstring(const QObject *obj);
Signature · 1 line
1.6.5.2.143.g8cc62
#5 lore raw

PATCH 5/8 Introduce QError

In reply to Luiz Capitulino

QError is a high-level data type which represents an exception, it stores the following error information:

- name A generic error name (eg. "ServiceUnavailable")
- description A detailed error description, which may contain
references to run-time error data

- filename       The file name of where the error occurred
- line number    The exact line number of the error
- run-time data  Run-time error data

The qerror_print() function should be used to properly format and print the stored information to the right place, that is, to stderr if the Monitor is not running, or to the Monitor's device otherwise.

The following functions are exported:

- qerror_new(): Create a new QError
- qerror_from_info(): Create a new QError from the specified error information
- qerror_print(): Print the specified QError

---
 Makefile  |    2 +-
 qerror.c  |  235 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qerror.h  |   39 ++++++++++
 qobject.h |    1 +
 4 files changed, 276 insertions(+), 1 deletions(-)
 create mode 100644 qerror.c
 create mode 100644 qerror.h
Makefile +1 -1
index 6fcbcaa..2dfaebd 100644
@@ -135,7 +135,7 @@ obj-y += buffered_file.o migration.o migration-tcp.o qemu-sockets.o
 obj-y += qemu-char.o aio.o savevm.o
 obj-y += msmouse.o ps2.o
 obj-y += qdev.o qdev-properties.o
-obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o qjson.o
+obj-y += qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o qjson.o qerror.o
 obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
qerror.c new file +235 -0

235 lines not on this page · raw message

qerror.h new file +39 -0
new file mode 100644
index 0000000..dc39dec
@@ -0,0 +1,39 @@
+/*
+ * QError header file.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef QERROR_H
+#define QERROR_H
+
+#include <stdarg.h>
+#include "qobject.h"
+
+typedef struct QError {
+    QObject_HEAD;
+    const char *name;   /* generic name */
+    const char *desc;   /* description */
+    int linenr;         /* line number */
+    const char *file;   /* file name */
+    QObject *data;      /* run-time data */
+} QError;
+
+QError *qerror_new(void);
+QError *qerror_from_info(const char *name, const char *file, int linenr,
+                         const char *desc, const char *fmt, va_list *va);
+void qerror_print(const QError *qerror);
+QError *qobject_to_qerror(const QObject *obj);
+
+/*
+ * Common error names
+ */
+#define QERR_DEV_NFOUND "DeviceNotFound"
+#define QERR_SER_UNAV   "ServiceUnavailable"
+
+#endif /* QERROR_H */
qobject.h +1 -0
index 167b607..838dddc 100644
@@ -43,6 +43,7 @@ typedef enum {
     QTYPE_QLIST,
     QTYPE_QFLOAT,
     QTYPE_QBOOL,
+    QTYPE_QERROR,
 } qtype_code;
 
 struct QObject;
Signature · 1 line
1.6.5.2.143.g8cc62
#6 lore raw

[Qemu-devel] Re: [PATCH 5/8] Introduce QError

qerror.h line 37

Quoted 2 lines · Luiz Capitulino
qerror.h new file +2 -0
+#define QERR_DEV_NFOUND "DeviceNotFound"
+#define QERR_SER_UNAV   "ServiceUnavailable"

I don't like packing names this way (what's wrong with DEVICE_NOT_FOUND and SERVICE_UNAVAILABLE?...), but otherwise the series looks fine.

Paolo

#7 lore raw

PATCH 6/8 monitor: QError support

In reply to Luiz Capitulino

This commit adds QError support in the Monitor.

A QError member is added to the Monitor struct. This new member stores error information and is also used to check if an error has occurred when the called handlers returns.

Additionally, a new macro called qemu_error_new() is introduced. It should be used in pace of qemu_error() to report errors.

When all conversion to qemu_error_new() is done, qemu_error() can be turned private.

Basically, Monitor's error flow is something like this:

1. An error occurs in the handler, it calls qemu_error_new()
2. qemu_error_new() builds a new QError object and stores it in the Monitor struct
3. The handler returns
4. Top level Monitor code checks the Monitor struct and calls qerror_print() to print the error

---
 monitor.c |   43 ++++++++++++++++++++++++++++++++++++++++++-
 sysemu.h  |    7 +++++++
 2 files changed, 49 insertions(+), 1 deletions(-)
monitor.c +42 -1
index 109ff5c..0e6e21b 100644
@@ -49,6 +49,7 @@
 #include "qlist.h"
 #include "qdict.h"
 #include "qstring.h"
+#include "qerror.h"
 
 //#define DEBUG
 //#define DEBUG_COMPLETION
@@ -103,6 +104,7 @@ struct Monitor {
     CPUState *mon_cpu;
     BlockDriverCompletionFunc *password_completion_cb;
     void *password_opaque;
+    QError *error;
     QLIST_HEAD(,mon_fd_t) fds;
     QLIST_ENTRY(Monitor) entry;
 };
@@ -3146,6 +3148,18 @@ fail:
     return NULL;
 }
 
+static inline int monitor_has_error(const Monitor *mon)
+{
+    return mon->error != NULL;
+}
+
+static void monitor_print_error(Monitor *mon)
+{
+    qerror_print(mon->error);
+    QDECREF(mon->error);
+    mon->error = NULL;
+}
+
 static void monitor_handle_command(Monitor *mon, const char *cmdline)
 {
     QDict *qdict;
@@ -3171,7 +3185,10 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline)
         cmd->mhandler.cmd(mon, qdict);
     }
 
-   qemu_errors_to_previous();
+    if (monitor_has_error(mon))
+        monitor_print_error(mon);
+
+    qemu_errors_to_previous();
 
 out:
     QDECREF(qdict);
@@ -3622,3 +3639,27 @@ void qemu_error(const char *fmt, ...)
         break;
     }
 }
+
+void qemu_error_full(const char *name, const char *file, int linenr,
+                     const char *desc, const char *fmt, ...)
+{
+    va_list va;
+    QError *qerror;
+
+    assert(qemu_error_sink != NULL);
+
+    va_start(va, fmt);
+    qerror = qerror_from_info(name, file, linenr, desc, fmt, &va);
+    va_end(va);
+
+    switch (qemu_error_sink->dest) {
+    case ERR_SINK_FILE:
+        qerror_print(qerror);
+        QDECREF(qerror);
+        break;
+    case ERR_SINK_MONITOR:
+        assert(qemu_error_sink->mon->error == NULL);
+        qemu_error_sink->mon->error = qerror;
+        break;
+    }
+}
sysemu.h +7 -0
index 96804b4..20ab709 100644
@@ -7,6 +7,7 @@
 #include "qemu-queue.h"
 #include "qemu-timer.h"
 #include "qdict.h"
+#include "qerror.h"
 
 #ifdef _WIN32
 #include <windows.h>
@@ -71,6 +72,12 @@ void qemu_errors_to_file(FILE *fp);
 void qemu_errors_to_mon(Monitor *mon);
 void qemu_errors_to_previous(void);
 void qemu_error(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
+void qemu_error_full(const char *name, const char *file, int linenr,
+                     const char *desc, const char *fmt, ...)
+                     __attribute__ ((format(printf, 5, 6)));
+
+#define qemu_error_new(name, desc, fmt, ...) \
+    qemu_error_full(name, __FILE__, __LINE__, desc, fmt, ## __VA_ARGS__)
 
 #ifdef _WIN32
 /* Polling handling */
Signature · 1 line
1.6.5.2.143.g8cc62
#8 lore raw

PATCH 7/8 qdev: Use QError for not found error

In reply to Luiz Capitulino
---
 hw/qdev.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
hw/qdev.c +3 -2
index c7884d0..db86cb2 100644
@@ -29,6 +29,7 @@
 #include "qdev.h"
 #include "sysemu.h"
 #include "monitor.h"
+#include "qerror.h"
 
 static int qdev_hotplug = 0;
 
@@ -176,8 +177,8 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     /* find driver */
     info = qdev_find_info(NULL, driver);
     if (!info) {
-        qemu_error("Device \"%s\" not found.  Try -device '?' for a list.\n",
-                   driver);
+        qemu_error_new(QERR_DEV_NFOUND, "device \"%(name)s\" not found",
+                       "{ 'name': %s }", driver);
         return NULL;
     }
     if (info->no_user) {
Signature · 1 line
1.6.5.2.143.g8cc62
#9 lore raw

PATCH 8/8 monitor: do_info_balloon(): use QError

In reply to Luiz Capitulino
---
 monitor.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
monitor.c +4 -3
index 0e6e21b..423c6b5 100644
@@ -1717,10 +1717,11 @@ static void do_info_balloon(Monitor *mon, QObject **ret_data)
 
     actual = qemu_balloon_status();
     if (kvm_enabled() && !kvm_has_sync_mmu())
-        monitor_printf(mon, "Using KVM without synchronous MMU, "
-                       "ballooning disabled\n");
+        qemu_error_new(QERR_SER_UNAV,
+                       "Using KVM without synchronous MMU, ballooning disabled",
+                       NULL);
     else if (actual == 0)
-        monitor_printf(mon, "Ballooning not activated in VM\n");
+        qemu_error_new(QERR_SER_UNAV, "Ballooning not activated in VM", NULL);
     else
         *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
 }
Signature · 1 line
1.6.5.2.143.g8cc62
#10 lore raw
In reply to Luiz Capitulino
Quoted 10 lines · Luiz Capitulino

Hi,

I can't remember seeing updated versions of a RFC series, but this should
prevent Anthony's scripts from merging these patches.

This new QError version has two major changes: the static error table has
been dropped and I'm using symbolic names instead of error codes.

Now, a call to:

Quoted 8 lines

monitor_printf(mon, "husb: host usb device %d.%d is already open\n",

               bus_num, addr);

Would become something like:

qemu_error_new('DeviceAlreadyOpen', "{ 'bus_num': %d, 'addr': %d }",

               bus_num, addr);

I mostly like this but this is not what the patches do :-)

Here's what I would like to see:

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

That gives us a nice simple interface with full error checking on the parameters.

For human readable strings, I'd suggest making a table somewhere else that looked like:

QErrorStringTable qerror_descriptions[] = {
{ QERR_DEVICE_ALREADY_OPEN, "This device at %(bus_num)d.%(addr)d is already open." },
...
};

There are a number of advantages to an approach like this. The table can be reused by both in the server and by a client.

Regards,

Anthony Liguori

#11 lore raw
Quoted 35 lines · On Wed, 11 Nov 2009 15:20:30 -0600 Anthony Liguori <[email protected]>…
Quoted 10 lines · Luiz Capitulino

Hi,

I can't remember seeing updated versions of a RFC series, but this should
prevent Anthony's scripts from merging these patches.

This new QError version has two major changes: the static error table has
been dropped and I'm using symbolic names instead of error codes.

Now, a call to:

Quoted 8 lines

monitor_printf(mon, "husb: host usb device %d.%d is already open\n",

               bus_num, addr);

Would become something like:

qemu_error_new('DeviceAlreadyOpen', "{ 'bus_num': %d, 'addr': %d }",

               bus_num, addr);

I mostly like this but this is not what the patches do :-)

Here's what I would like to see:

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

That gives us a nice simple interface with full error checking on the parameters.

For human readable strings, I'd suggest making a table somewhere else that looked like:

Quoted 8 lines

QErrorStringTable qerror_descriptions[] = {
{ QERR_DEVICE_ALREADY_OPEN, "This device at %(bus_num)d.%(addr)d is already open." },
...
};

There are a number of advantages to an approach like this. The table can be reused by both in the server and by a client.

I'm ok with this, the only comment I have is that 'class' should
be a member of QErrorStringTable, so that we can use it to lookup the table.

#12 lore raw
In reply to Anthony Liguori
Quoted 16 lines · On Wed, 11 Nov 2009 15:20:30 -0600 Anthony Liguori <[email protected]>…
Quoted 15 lines · Luiz Capitulino

Hi,

I can't remember seeing updated versions of a RFC series, but this should
prevent Anthony's scripts from merging these patches.

This new QError version has two major changes: the static error table has
been dropped and I'm using symbolic names instead of error codes.

Now, a call to:

monitor_printf(mon, "husb: host usb device %d.%d is already open\n",

               bus_num, addr);

Would become something like:

Quoted 7 lines
Quoted 3 lines

qemu_error_new('DeviceAlreadyOpen', "{ 'bus_num': %d, 'addr': %d }",

               bus_num, addr);

I mostly like this but this is not what the patches do :-)

Here's what I would like to see:

Thinking about this a little more I see some problems with it.

Quoted 4 lines

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

What about DeviceAlreadyOpen errors with a different argument list?

Quoted 2 lines

That gives us a nice simple interface with full error checking on the parameters.

I've said this is not so simple because people writing those macros
would find out that the 'class' or 'data' _keys_ are missing or incorrect only at run-time, when the error is triggered.

Quoted 8 lines

For human readable strings, I'd suggest making a table somewhere else that looked like:

QErrorStringTable qerror_descriptions[] = {
{ QERR_DEVICE_ALREADY_OPEN, "This device at %(bus_num)d.%(addr)d is already open." },
...
};

How do you suggest we lookup the table? Doing a strcmp() on
QERR_DEVICE_ALREADY_OPEN?

Quoted 2 lines

There are a number of advantages to an approach like this. The table can be reused by both in the server and by a client.

My suggestions on both problems makes me willing go back to my initial
series, which had a table indexed by an error number.

#13 lore raw
Quoted 8 lines · Luiz Capitulino
Quoted 5 lines

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

What about DeviceAlreadyOpen errors with a different argument list?

Why would you have this? That would seem like a problem to me. I think the errors need to be very well structured (just like everything else in QMP).

Quoted 7 lines
Quoted 2 lines

That gives us a nice simple interface with full error checking on the parameters.

I've said this is not so simple because people writing those macros
would find out that the 'class' or 'data' _keys_ are missing or incorrect only at run-time, when the error is triggered.

Sure but introducing new types of errors is not the common case. Using existing errors is the common case.

Quoted 11 lines
Quoted 7 lines

For human readable strings, I'd suggest making a table somewhere else that looked like:

QErrorStringTable qerror_descriptions[] = {
{ QERR_DEVICE_ALREADY_OPEN, "This device at %(bus_num)d.%(addr)d is already open." },
...
};

How do you suggest we lookup the table? Doing a strcmp() on
QERR_DEVICE_ALREADY_OPEN?

We can either change the index on the table to be just the class code or find something more clever.

Quoted 6 lines
Quoted 2 lines

There are a number of advantages to an approach like this. The table can be reused by both in the server and by a client.

My suggestions on both problems makes me willing go back to my initial
series, which had a table indexed by an error number.

I don't understand why.

Signature · 3 lines
Regards,

Anthony Liguori
#14 lore raw
Quoted 8 lines · On Thu, 12 Nov 2009 08:44:03 -0600 Anthony Liguori…
Quoted 7 lines · Luiz Capitulino
Quoted 6 lines

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

Quoted 6 lines
Quoted 2 lines

What about DeviceAlreadyOpen errors with a different argument list?

Why would you have this? That would seem like a problem to me. I think the errors need to be very well structured (just like everything else in QMP).

This can happen with errors that carry specific info which are different
among subsystems, eg. USB device info vs. PCI device info.

We could have 'USBDeviceAlreadyOpen', but then I think the class
attribute will lose generality.

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

That gives us a nice simple interface with full error checking on the parameters.

I've said this is not so simple because people writing those macros
would find out that the 'class' or 'data' _keys_ are missing or incorrect only at run-time, when the error is triggered.

Sure but introducing new types of errors is not the common case. Using existing errors is the common case.

Right, although there's a long road until we stabilize.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

For human readable strings, I'd suggest making a table somewhere else that looked like:

QErrorStringTable qerror_descriptions[] = {
{ QERR_DEVICE_ALREADY_OPEN, "This device at %(bus_num)d.%(addr)d is already open." },
...
};

Quoted 6 lines
Quoted 3 lines

How do you suggest we lookup the table? Doing a strcmp() on
QERR_DEVICE_ALREADY_OPEN?

We can either change the index on the table to be just the class code or find something more clever.

I'm working on this and trying to find something.

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

There are a number of advantages to an approach like this. The table can be reused by both in the server and by a client.

My suggestions on both problems makes me willing go back to my initial
series, which had a table indexed by an error number.

I don't understand why.

I've found other problems with it, let's pretend I didn't mention it. :)

#15 lore raw
Quoted 11 lines · Luiz Capitulino
Quoted 7 lines · On Thu, 12 Nov 2009 08:44:03 -0600 Anthony Liguori…
Quoted 5 lines · Luiz Capitulino
Quoted 5 lines

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', 'data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

What about DeviceAlreadyOpen errors with a different argument list?

Why would you have this? That would seem like a problem to me. I think the errors need to be very well structured (just like everything else in QMP).

This can happen with errors that carry specific info which are different
among subsystems, eg. USB device info vs. PCI device info.

This example demonstrates the problem with this. You haven't included enough information in the error to properly distinguish what happened. I would suggest changing to:

'data' : { 'device_type' : %s, 'addr': %s }

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, "pci", addr);

Or, if you really wanted to get fancy:

'data' : { 'device_type': %s, 'addr': %p }

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, "pci",

                           qobject_from_jsonf("{'bus': %d, 'slot': %d, 

'function': %d}", bus, dev, fn));

Personally, I'd stick with the first one though.

Signature · 3 lines
Regards,

Anthony Liguori
#16 lore raw
In reply to Anthony Liguori
Quoted 19 lines · Anthony Liguori
Quoted 9 lines · Luiz Capitulino
Quoted 6 lines

#define QERR_DEVICE_ALREADY_OPEN "{'class': 'DeviceAlreadyOpen', data' : {'bus_num': %d, 'addr': %d}"

qemu_error_new(QERR_DEVICE_ALREADY_OPEN, bus_num, addr);

What about DeviceAlreadyOpen errors with a different argument list?

Why would you have this? That would seem like a problem to me. I think the errors need to be very well structured (just like everything else in QMP).

Quoted 4 lines
Quoted 3 lines

That gives us a nice simple interface with full error checking on the parameters.

Quoted 7 lines
Quoted 4 lines

I've said this is not so simple because people writing those macros
would find out that the 'class' or 'data' _keys_ are missing or incorrect only at run-time, when the error is triggered.

Sure but introducing new types of errors is not the common case. Using existing errors is the common case.

Careful. (Valid) reuse of existing errors may be more common, but proper creation of new errors is important. If you make that too hard compared to reuse, you risk abuse of existing errors for cases where they don't quite fit.

[...]