]> git.proxmox.com Git - qemu.git/blob - error.c
ppc_prep: fix pci config space initialization
[qemu.git] / error.c
1 /*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2. See
10 * the COPYING.LIB file in the top-level directory.
11 */
12
13 #include "qemu-common.h"
14 #include "error.h"
15 #include "error_int.h"
16 #include "qemu-objects.h"
17 #include "qerror.h"
18
19 struct Error
20 {
21 QDict *obj;
22 const char *fmt;
23 char *msg;
24 };
25
26 void error_set(Error **errp, const char *fmt, ...)
27 {
28 Error *err;
29 va_list ap;
30
31 if (errp == NULL) {
32 return;
33 }
34
35 err = g_malloc0(sizeof(*err));
36
37 va_start(ap, fmt);
38 err->obj = qobject_to_qdict(qobject_from_jsonv(fmt, &ap));
39 va_end(ap);
40 err->fmt = fmt;
41
42 *errp = err;
43 }
44
45 bool error_is_set(Error **errp)
46 {
47 return (errp && *errp);
48 }
49
50 const char *error_get_pretty(Error *err)
51 {
52 if (err->msg == NULL) {
53 QString *str;
54 str = qerror_format(err->fmt, err->obj);
55 err->msg = g_strdup(qstring_get_str(str));
56 QDECREF(str);
57 }
58
59 return err->msg;
60 }
61
62 const char *error_get_field(Error *err, const char *field)
63 {
64 if (strcmp(field, "class") == 0) {
65 return qdict_get_str(err->obj, field);
66 } else {
67 QDict *dict = qdict_get_qdict(err->obj, "data");
68 return qdict_get_str(dict, field);
69 }
70 }
71
72 QDict *error_get_data(Error *err)
73 {
74 QDict *data = qdict_get_qdict(err->obj, "data");
75 QINCREF(data);
76 return data;
77 }
78
79 void error_set_field(Error *err, const char *field, const char *value)
80 {
81 QDict *dict = qdict_get_qdict(err->obj, "data");
82 return qdict_put(dict, field, qstring_from_str(value));
83 }
84
85 void error_free(Error *err)
86 {
87 if (err) {
88 QDECREF(err->obj);
89 g_free(err->msg);
90 g_free(err);
91 }
92 }
93
94 bool error_is_type(Error *err, const char *fmt)
95 {
96 const char *error_class;
97 char *ptr;
98 char *end;
99
100 ptr = strstr(fmt, "'class': '");
101 assert(ptr != NULL);
102 ptr += strlen("'class': '");
103
104 end = strchr(ptr, '\'');
105 assert(end != NULL);
106
107 error_class = error_get_field(err, "class");
108 if (strlen(error_class) != end - ptr) {
109 return false;
110 }
111
112 return strncmp(ptr, error_class, end - ptr) == 0;
113 }
114
115 void error_propagate(Error **dst_err, Error *local_err)
116 {
117 if (dst_err) {
118 *dst_err = local_err;
119 } else if (local_err) {
120 error_free(local_err);
121 }
122 }
123
124 QObject *error_get_qobject(Error *err)
125 {
126 QINCREF(err->obj);
127 return QOBJECT(err->obj);
128 }
129
130 void error_set_qobject(Error **errp, QObject *obj)
131 {
132 Error *err;
133 if (errp == NULL) {
134 return;
135 }
136 err = g_malloc0(sizeof(*err));
137 err->obj = qobject_to_qdict(obj);
138 qobject_incref(obj);
139
140 *errp = err;
141 }