Skip to contentSkip to conversation

: QError v3

PATCH In review Luiz Capitulino 2 participants 12 messages Nov 12, 2009 – Nov 13, 2009 Kbuild

lore
Share & tools
lore
#1 lore raw

PATCH 1/9 QJSON: Introduce qobject_from_json_va()

Accepts a va_list, will be used by QError.

---
 qjson.c |   14 ++++++++++++++
 qjson.h |    2 ++
 2 files changed, 16 insertions(+), 0 deletions(-)
qjson.c +14 -0
index 7270909..491b61e 100644
@@ -64,6 +64,20 @@ QObject *qobject_from_jsonf(const char *string, ...)
     return state.result;
 }
 
+QObject *qobject_from_json_va(const char *string, va_list *ap)
+{
+    JSONParsingState state = {};
+
+    state.ap = ap;
+
+    json_message_parser_init(&state.parser, parse_json);
+    json_message_parser_feed(&state.parser, string, strlen(string));
+    json_message_parser_flush(&state.parser);
+    json_message_parser_destroy(&state.parser);
+
+    return state.result;
+}
+
 typedef struct ToJsonIterState
 {
     int count;
qjson.h +2 -0
index 7fce742..8b89e41 100644
@@ -14,12 +14,14 @@
 #ifndef QJSON_H
 #define QJSON_H
 
+#include <stdarg.h>
 #include "qobject.h"
 #include "qstring.h"
 
 QObject *qobject_from_json(const char *string);
 QObject *qobject_from_jsonf(const char *string, ...)
     __attribute__((__format__ (__printf__, 1, 2)));
+QObject *qobject_from_json_va(const char *string, va_list *ap);
 
 QString *qobject_to_json(const QObject *obj);
 
Signature · 1 line
1.6.5.2.155.gbb47
#2 lore raw

Re: [Qemu-devel] [PATCH 1/9] QJSON: Introduce qobject_from_json_va()

Quoted 21 lines · Luiz Capitulino

Accepts a va_list, will be used by QError.

---
 qjson.c |   14 ++++++++++++++
 qjson.h |    2 ++
 2 files changed, 16 insertions(+), 0 deletions(-)
qjson.c +5 -0
index 7270909..491b61e 100644
@@ -64,6 +64,20 @@ QObject *qobject_from_jsonf(const char *string, ...)
     return state.result;
 }
 
+QObject *qobject_from_json_va(const char *string, va_list *ap)
+{
+    JSONParsingState state = {};
+
+    state.ap = ap;

qjson.c QObject *qobject_from_jsonf(const char *string, ...) · line 81

Quoted 10 lines
qjson.c +9 -0
+
+    json_message_parser_init(&state.parser, parse_json);
+    json_message_parser_feed(&state.parser, string, strlen(string));
+    json_message_parser_flush(&state.parser);
+    json_message_parser_destroy(&state.parser);
+
+    return state.result;
+}
+
 

qobject_from_jsonf() should just call qobject_from_json_va().

qjson.h line 18

Quoted 13 lines
qjson.c +0 -0
 typedef struct ToJsonIterState
 {
     int count;
qjson.h +1 -0
index 7fce742..8b89e41 100644
@@ -14,12 +14,14 @@
 #ifndef QJSON_H
 #define QJSON_H
 
+#include <stdarg.h>
 

qemu-common.h is a better way to get stdarg.h

qjson.h line 26

Quoted 11 lines
qjson.h +1 -0
 #include "qobject.h"
 #include "qstring.h"
 
 QObject *qobject_from_json(const char *string);
 QObject *qobject_from_jsonf(const char *string, ...)
     __attribute__((__format__ (__printf__, 1, 2)));
+QObject *qobject_from_json_va(const char *string, va_list *ap);
 
 QString *qobject_to_json(const QObject *obj);

Regards,

Anthony Liguori

#3 lore raw

Re: [Qemu-devel] [PATCH 1/9] QJSON: Introduce qobject_from_json_va()

Quoted 26 lines · On Thu, 12 Nov 2009 15:39:14 -0600 Anthony Liguori <[email protected]>…
Quoted 25 lines · Luiz Capitulino

Accepts a va_list, will be used by QError.

---
 qjson.c |   14 ++++++++++++++
 qjson.h |    2 ++
 2 files changed, 16 insertions(+), 0 deletions(-)
qjson.c +9 -0
index 7270909..491b61e 100644
@@ -64,6 +64,20 @@ QObject *qobject_from_jsonf(const char *string, ...)
     return state.result;
 }
 
+QObject *qobject_from_json_va(const char *string, va_list *ap)
+{
+    JSONParsingState state = {};
+
+    state.ap = ap;
+
+    json_message_parser_init(&state.parser, parse_json);
+    json_message_parser_feed(&state.parser, string, strlen(string));
+    json_message_parser_flush(&state.parser);
Quoted 8 lines

qjson.c QObject *qobject_from_jsonf(const char *string, ...)

Quoted 6 lines
qjson.c +5 -0
+    json_message_parser_destroy(&state.parser);
+
+    return state.result;
+}
+
 

qobject_from_jsonf() should just call qobject_from_json_va().

Right.

Quoted 11 lines
Quoted 11 lines
qjson.c +0 -0
 typedef struct ToJsonIterState
 {
     int count;
qjson.h +0 -0
index 7fce742..8b89e41 100644
@@ -14,12 +14,14 @@
 #ifndef QJSON_H
 #define QJSON_H
 
Quoted 4 lines

qjson.h line 20

Quoted 2 lines
qjson.h +1 -0
+#include <stdarg.h>
 

qemu-common.h is a better way to get stdarg.h

Why? I usually don't include qemu-common.h when all I need
are one or two headers.

#4 lore raw

PATCH 2/9 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.155.gbb47
#5 lore raw

PATCH 3/9 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.155.gbb47
#6 lore raw

PATCH 4/9 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.155.gbb47
#7 lore raw

PATCH 5/9 Introduce QError

In reply to Luiz Capitulino

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

- class Error class 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  Any run-time error data

This commit adds the basic interface plus two error classes, one for 'device not found' errors and another one for 'service unavailable' errors.

---
 Makefile  |    2 +-
 qerror.c  |  283 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qerror.h  |   46 ++++++++++
 qobject.h |    1 +
 4 files changed, 331 insertions(+), 1 deletions(-)
 create mode 100644 qerror.c
 create mode 100644 qerror.h
Makefile +1 -1
index 4998226..98625e7 100644
@@ -138,7 +138,7 @@ 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 json-lexer.o
-obj-y += json-streamer.o json-parser.o qjson.o
+obj-y += json-streamer.o json-parser.o qjson.o qerror.o
 obj-y += qemu-config.o
 
 obj-$(CONFIG_BRLAPI) += baum.o
qerror.c new file +283 -0

283 lines not on this page · raw message

qerror.h new file +46 -0

46 lines not on this page · raw message

qobject.h +1 -0
index 2270ec1..07de211 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.155.gbb47
#8 lore raw

PATCH 6/9 QJSON: Fix compile error

In reply to Luiz Capitulino

Add QTYPE_QERROR handling in the to_json() switch, otherwise GCC will complain.

We just abort(), as QError design is not finished yet.

---
 qjson.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
qjson.c +3 -0
index 491b61e..ca4fe7c 100644
@@ -239,6 +239,9 @@ static void to_json(const QObject *obj, QString *str)
     }
     case QTYPE_NONE:
         break;
+    case QTYPE_QERROR:
+        fprintf(stderr, "qerror can't be emitted yet\n");
+        abort();
     }
 }
 
Signature · 1 line
1.6.5.2.155.gbb47
#9 lore raw

PATCH 7/9 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 |   42 +++++++++++++++++++++++++++++++++++++++++-
 sysemu.h  |    6 ++++++
 2 files changed, 47 insertions(+), 1 deletions(-)
monitor.c +41 -1
index 132fb6e..a8fd552 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,26 @@ void qemu_error(const char *fmt, ...)
         break;
     }
 }
+
+void qemu_error_full(const char *file, int linenr, const char *fmt, ...)
+{
+    va_list va;
+    QError *qerror;
+
+    assert(qemu_error_sink != NULL);
+
+    va_start(va, fmt);
+    qerror = qerror_from_info(file, linenr, 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 +6 -0
index 96804b4..5891def 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,11 @@ 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 *file, int linenr, const char *fmt, ...)
+                     __attribute__ ((format(printf, 3, 4)));
+
+#define qemu_error_new(fmt, ...) \
+    qemu_error_full(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
 
 #ifdef _WIN32
 /* Polling handling */
Signature · 1 line
1.6.5.2.155.gbb47
#10 lore raw

PATCH 8/9 qdev: Use QError for 'device not found' error

In reply to Luiz Capitulino
---
 hw/qdev.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
hw/qdev.c +2 -2
index d19d531..875ca50 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,7 @@ 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_DEVICE_NOT_FOUND, driver);
         return NULL;
     }
     if (info->no_user) {
Signature · 1 line
1.6.5.2.155.gbb47
#11 lore raw

PATCH 9/9 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 a8fd552..76cb187 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_SERVICE_UNAVAILABLE,
+                      "Using KVM without synchronous MMU, ballooning disabled");
     else if (actual == 0)
-        monitor_printf(mon, "Ballooning not activated in VM\n");
+        qemu_error_new(QERR_SERVICE_UNAVAILABLE,
+                       "Ballooning not activated in VM");
     else
         *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
 }
Signature · 1 line
1.6.5.2.155.gbb47