blob: fc8331aa67402c0924d38a2e51edfdd18364d6ff [file] [log] [blame]
David 'Digit' Turner2910f182010-05-10 18:48:35 -07001/*
David Turnerfd4c0072010-09-09 21:23:36 +02002 * QError Module
David 'Digit' Turner2910f182010-05-10 18:48:35 -07003 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 */
David Turnerfd4c0072010-09-09 21:23:36 +020012
David 'Digit' Turner6af67652013-12-14 23:49:32 +010013#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010014#include "qapi/qmp/qjson.h"
15#include "qapi/qmp/qerror.h"
David 'Digit' Turner2910f182010-05-10 18:48:35 -070016#include "qemu-common.h"
17
18static void qerror_destroy_obj(QObject *obj);
19
20static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
23};
24
25/**
David 'Digit' Turner2910f182010-05-10 18:48:35 -070026 * qerror_new(): Create a new QError
27 *
28 * Return strong reference.
29 */
David 'Digit' Turner910aea92014-01-15 16:53:38 +010030static QError *qerror_new(void)
David 'Digit' Turner2910f182010-05-10 18:48:35 -070031{
32 QError *qerr;
33
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +010034 qerr = g_malloc0(sizeof(*qerr));
David 'Digit' Turner2910f182010-05-10 18:48:35 -070035 QOBJECT_INIT(qerr, &qerror_type);
36
37 return qerr;
38}
39
David 'Digit' Turner2910f182010-05-10 18:48:35 -070040/**
41 * qerror_from_info(): Create a new QError from error information
42 *
David 'Digit' Turner2910f182010-05-10 18:48:35 -070043 * Return strong reference.
44 */
David 'Digit' Turner910aea92014-01-15 16:53:38 +010045static QError * GCC_FMT_ATTR(2, 0)
46qerror_from_info(ErrorClass err_class, const char *fmt, va_list *va)
David 'Digit' Turner2910f182010-05-10 18:48:35 -070047{
48 QError *qerr;
49
50 qerr = qerror_new();
David Turnerfd4c0072010-09-09 21:23:36 +020051 loc_save(&qerr->loc);
David 'Digit' Turner2910f182010-05-10 18:48:35 -070052
David 'Digit' Turner910aea92014-01-15 16:53:38 +010053 qerr->err_msg = g_strdup_vprintf(fmt, *va);
54 qerr->err_class = err_class;
David 'Digit' Turner2910f182010-05-10 18:48:35 -070055
56 return qerr;
57}
58
David 'Digit' Turner2910f182010-05-10 18:48:35 -070059/**
60 * qerror_human(): Format QError data into human-readable string.
David 'Digit' Turner2910f182010-05-10 18:48:35 -070061 */
62QString *qerror_human(const QError *qerror)
63{
David 'Digit' Turner910aea92014-01-15 16:53:38 +010064 return qstring_from_str(qerror->err_msg);
David 'Digit' Turner2910f182010-05-10 18:48:35 -070065}
66
67/**
68 * qerror_print(): Print QError data
69 *
70 * This function will print the member 'desc' of the specified QError object,
David Turnerfd4c0072010-09-09 21:23:36 +020071 * it uses error_report() for this, so that the output is routed to the right
David 'Digit' Turner2910f182010-05-10 18:48:35 -070072 * place (ie. stderr or Monitor's device).
73 */
David 'Digit' Turner910aea92014-01-15 16:53:38 +010074static void qerror_print(QError *qerror)
David 'Digit' Turner2910f182010-05-10 18:48:35 -070075{
76 QString *qstring = qerror_human(qerror);
David Turnerfd4c0072010-09-09 21:23:36 +020077 loc_push_restore(&qerror->loc);
78 error_report("%s", qstring_get_str(qstring));
79 loc_pop(&qerror->loc);
David 'Digit' Turner2910f182010-05-10 18:48:35 -070080 QDECREF(qstring);
81}
82
David 'Digit' Turner910aea92014-01-15 16:53:38 +010083void qerror_report(ErrorClass eclass, const char *fmt, ...)
David Turnerfd4c0072010-09-09 21:23:36 +020084{
85 va_list va;
86 QError *qerror;
87
88 va_start(va, fmt);
David 'Digit' Turner910aea92014-01-15 16:53:38 +010089 qerror = qerror_from_info(eclass, fmt, &va);
David Turnerfd4c0072010-09-09 21:23:36 +020090 va_end(va);
91
92 if (monitor_cur_is_qmp()) {
93 monitor_set_error(cur_mon, qerror);
94 } else {
95 qerror_print(qerror);
96 QDECREF(qerror);
97 }
98}
99
David 'Digit' Turner910aea92014-01-15 16:53:38 +0100100/* Evil... */
101struct Error
102{
103 char *msg;
104 ErrorClass err_class;
105};
106
107void qerror_report_err(Error *err)
108{
109 QError *qerr;
110
111 qerr = qerror_new();
112 loc_save(&qerr->loc);
113 qerr->err_msg = g_strdup(err->msg);
114 qerr->err_class = err->err_class;
115
116 if (monitor_cur_is_qmp()) {
117 monitor_set_error(cur_mon, qerr);
118 } else {
119 qerror_print(qerr);
120 QDECREF(qerr);
121 }
122}
123
124void assert_no_error(Error *err)
125{
126 if (err) {
127 qerror_report_err(err);
128 abort();
129 }
130}
131
David 'Digit' Turner2910f182010-05-10 18:48:35 -0700132/**
133 * qobject_to_qerror(): Convert a QObject into a QError
134 */
David 'Digit' Turner910aea92014-01-15 16:53:38 +0100135static QError *qobject_to_qerror(const QObject *obj)
David 'Digit' Turner2910f182010-05-10 18:48:35 -0700136{
137 if (qobject_type(obj) != QTYPE_QERROR) {
138 return NULL;
139 }
140
141 return container_of(obj, QError, base);
142}
143
144/**
145 * qerror_destroy_obj(): Free all memory allocated by a QError
146 */
147static void qerror_destroy_obj(QObject *obj)
148{
149 QError *qerr;
150
151 assert(obj != NULL);
152 qerr = qobject_to_qerror(obj);
153
David 'Digit' Turner910aea92014-01-15 16:53:38 +0100154 g_free(qerr->err_msg);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100155 g_free(qerr);
David 'Digit' Turner2910f182010-05-10 18:48:35 -0700156}