2011-07-19 21:50:42 +02:00
|
|
|
#
|
|
|
|
# QAPI command marshaller generator
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2011
|
qapi: Unify type bypass and add tests
For a few QMP commands, we are forced to pass an arbitrary type
without tracking it properly in QAPI. Among the existing clients,
this unnamed type was spelled 'dict', 'visitor', and '**'; this
patch standardizes on '**', matching the documentation changes
earlier in the series.
Meanwhile, for the 'gen' key, we have been ignoring the value,
although the schema consistently used "'no'" ('success-response'
was hard-coded to checking for 'no'). But now that we can support
a literal "false" in the schema, we might as well use that rather
than ignoring the value or special-casing a random string. Note
that these are one-way switches (use of 'gen':true is not the same
as omitting 'gen'). Also, the use of '**' requires 'gen':false,
but the use of 'gen':false does not mandate the use of '**'.
There is no difference to the generated code. Add some tests on
what we'd like to guarantee, although it will take later patches
to clean up test results and actually enforce the use of a bool
parameter.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-05-04 17:05:19 +02:00
|
|
|
# Copyright (C) 2014-2015 Red Hat, Inc.
|
2011-07-19 21:50:42 +02:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
# Michael Roth <mdroth@linux.vnet.ibm.com>
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
2011-07-19 21:50:42 +02:00
|
|
|
#
|
2014-03-01 08:40:34 +01:00
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
from ordereddict import OrderedDict
|
|
|
|
from qapi import *
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
import re
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
def generate_command_decl(name, args, ret_type):
|
|
|
|
arglist=""
|
2015-05-04 17:05:33 +02:00
|
|
|
for argname, argtype, optional in parse_args(args):
|
2014-06-10 13:25:52 +02:00
|
|
|
argtype = c_type(argtype, is_param=True)
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
2015-05-14 14:50:48 +02:00
|
|
|
arglist += "bool has_%s, " % c_name(argname)
|
|
|
|
arglist += "%s %s, " % (argtype, c_name(argname))
|
2011-07-19 21:50:42 +02:00
|
|
|
return mcgen('''
|
|
|
|
%(ret_type)s qmp_%(name)s(%(args)sError **errp);
|
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
ret_type=c_type(ret_type), name=c_name(name),
|
2015-06-27 17:49:34 +02:00
|
|
|
args=arglist)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-06-27 16:48:14 +02:00
|
|
|
def gen_err_check(err):
|
|
|
|
if not err:
|
|
|
|
return ''
|
|
|
|
return mcgen('''
|
|
|
|
if (%(err)s) {
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2015-06-27 16:48:14 +02:00
|
|
|
''',
|
|
|
|
err=err)
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
def gen_sync_call(name, args, ret_type):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = ""
|
|
|
|
arglist=""
|
|
|
|
retval=""
|
|
|
|
if ret_type:
|
|
|
|
retval = "retval = "
|
2015-05-04 17:05:33 +02:00
|
|
|
for argname, argtype, optional in parse_args(args):
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
2015-05-14 14:50:48 +02:00
|
|
|
arglist += "has_%s, " % c_name(argname)
|
|
|
|
arglist += "%s, " % (c_name(argname))
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
push_indent()
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = mcgen('''
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
%(retval)sqmp_%(name)s(%(args)s&local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-06-27 17:49:34 +02:00
|
|
|
name=c_name(name), args=arglist, retval=retval)
|
2011-07-19 21:50:42 +02:00
|
|
|
if ret_type:
|
2015-06-27 17:49:34 +02:00
|
|
|
ret += gen_err_check('local_err')
|
2015-06-27 17:21:12 +02:00
|
|
|
ret += mcgen('''
|
|
|
|
|
|
|
|
qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-06-27 17:21:12 +02:00
|
|
|
c_name=c_name(name))
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
pop_indent()
|
2015-06-27 17:49:34 +02:00
|
|
|
return ret
|
2011-07-19 21:50:42 +02:00
|
|
|
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
def gen_visitor_input_containers_decl(args):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = ""
|
|
|
|
|
|
|
|
push_indent()
|
|
|
|
if len(args) > 0:
|
|
|
|
ret += mcgen('''
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
|
2011-07-19 21:50:42 +02:00
|
|
|
QapiDeallocVisitor *md;
|
|
|
|
Visitor *v;
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
pop_indent()
|
|
|
|
|
2015-06-27 17:49:34 +02:00
|
|
|
return ret
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
def gen_visitor_input_vars_decl(args):
|
|
|
|
ret = ""
|
|
|
|
push_indent()
|
2015-05-04 17:05:33 +02:00
|
|
|
for argname, argtype, optional in parse_args(args):
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
|
|
|
ret += mcgen('''
|
|
|
|
bool has_%(argname)s = false;
|
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
argname=c_name(argname))
|
2014-06-10 13:25:53 +02:00
|
|
|
if is_c_ptr(argtype):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret += mcgen('''
|
|
|
|
%(argtype)s %(argname)s = NULL;
|
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
argname=c_name(argname), argtype=c_type(argtype))
|
2011-07-19 21:50:42 +02:00
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
qapi: zero-initialize all QMP command parameters
In general QMP command parameter values are specified by consumers of the
QMP/HMP interface, but in the case of optional parameters these values may
be left uninitialized.
It is considered a bug for code to make use of optional parameters that have
not been flagged as being present by the marshalling code (via corresponding
has_<parameter> parameter), however our marshalling code will still pass
these uninitialized values on to the corresponding QMP function (to then
be ignored). Some compilers (clang in particular) consider this unsafe
however, and generate warnings as a result. As reported by Peter Maydell:
This is something clang's -fsanitize=undefined spotted. The
code generated by qapi-commands.py in qmp-marshal.c for
qmp_marshal_* functions where there are some optional
arguments looks like this:
bool has_force = false;
bool force;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_start_optional(v, &has_force, "force", errp);
if (has_force) {
visit_type_bool(v, &force, "force", errp);
}
visit_end_optional(v, errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_eject(device, has_force, force, errp);
In the case where has_force is false, we never initialize
force, but then we use it by passing it to qmp_eject.
I imagine we don't then actually use the value, but clang
complains in particular for 'bool' variables because the value
that ends up being loaded from memory for 'force' is not either
0 or 1 (being uninitialized stack contents).
Fix this by initializing all QMP command parameters to {0} in the
marshalling code prior to passing them on to the QMP functions.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-20 19:20:39 +02:00
|
|
|
%(argtype)s %(argname)s = {0};
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
argname=c_name(argname), argtype=c_type(argtype))
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
pop_indent()
|
2015-06-27 17:49:34 +02:00
|
|
|
return ret
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2014-05-07 09:53:44 +02:00
|
|
|
def gen_visitor_input_block(args, dealloc=False):
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = ""
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
errparg = '&local_err'
|
|
|
|
errarg = 'local_err'
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
if len(args) == 0:
|
|
|
|
return ret
|
|
|
|
|
|
|
|
push_indent()
|
|
|
|
|
|
|
|
if dealloc:
|
qapi: qapi-commands: fix possible leaks on visitor dealloc
In qmp-marshal.c the dealloc visitor calls use the same errp
pointer of the input visitor calls. This means that if any of
the input visitor calls fails, then the dealloc visitor will
return early, before freeing the object's memory.
Here's an example, consider this code:
int qmp_marshal_input_block_passwd(Monitor *mon, const QDict *qdict, QObject **ret)
{
[...]
char * device = NULL;
char * password = NULL;
mi = qmp_input_visitor_new_strict(QOBJECT(args));
v = qmp_input_get_visitor(mi);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qmp_input_visitor_cleanup(mi);
if (error_is_set(errp)) {
goto out;
}
qmp_block_passwd(device, password, errp);
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", errp);
visit_type_str(v, &password, "password", errp);
qapi_dealloc_visitor_cleanup(md);
[...]
return 0;
}
Consider errp != NULL when the out label is reached, we're going
to leak device and password.
This patch fixes this by always passing errp=NULL for dealloc
visitors, meaning that we always try to free them regardless of
any previous failure. The above example would then be:
out:
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
visit_type_str(v, &device, "device", NULL);
visit_type_str(v, &password, "password", NULL);
qapi_dealloc_visitor_cleanup(md);
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>
2013-07-11 20:26:56 +02:00
|
|
|
errparg = 'NULL'
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
errarg = None;
|
2011-07-19 21:50:42 +02:00
|
|
|
ret += mcgen('''
|
2014-05-07 09:53:44 +02:00
|
|
|
qmp_input_visitor_cleanup(mi);
|
2011-07-19 21:50:42 +02:00
|
|
|
md = qapi_dealloc_visitor_new();
|
|
|
|
v = qapi_dealloc_get_visitor(md);
|
|
|
|
''')
|
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
|
|
|
v = qmp_input_get_visitor(mi);
|
2014-05-07 09:53:44 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-05-04 17:05:33 +02:00
|
|
|
for argname, argtype, optional in parse_args(args):
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
|
|
|
ret += mcgen('''
|
2014-05-07 09:53:46 +02:00
|
|
|
visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
c_name=c_name(argname), name=argname, errp=errparg)
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
ret += gen_err_check(errarg)
|
|
|
|
ret += mcgen('''
|
|
|
|
if (has_%(c_name)s) {
|
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
c_name=c_name(argname))
|
2011-07-19 21:50:42 +02:00
|
|
|
push_indent()
|
|
|
|
ret += mcgen('''
|
2015-05-14 14:51:01 +02:00
|
|
|
visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
c_name=c_name(argname), name=argname, argtype=argtype,
|
2015-05-14 14:51:01 +02:00
|
|
|
visitor=type_name(argtype), errp=errparg)
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
ret += gen_err_check(errarg)
|
2011-07-19 21:50:42 +02:00
|
|
|
if optional:
|
|
|
|
pop_indent()
|
|
|
|
ret += mcgen('''
|
|
|
|
}
|
2014-05-07 09:53:46 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
if dealloc:
|
|
|
|
ret += mcgen('''
|
|
|
|
qapi_dealloc_visitor_cleanup(md);
|
|
|
|
''')
|
|
|
|
pop_indent()
|
2015-06-27 17:49:34 +02:00
|
|
|
return ret
|
2011-07-19 21:50:42 +02:00
|
|
|
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
def gen_marshal_output(name, ret_type):
|
2011-07-19 21:50:42 +02:00
|
|
|
if not ret_type:
|
|
|
|
return ""
|
2011-09-02 19:34:46 +02:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = mcgen('''
|
|
|
|
static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
|
|
|
|
{
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
Error *local_err = NULL;
|
2011-07-19 21:50:42 +02:00
|
|
|
QmpOutputVisitor *mo = qmp_output_visitor_new();
|
2014-05-07 09:53:44 +02:00
|
|
|
QapiDeallocVisitor *md;
|
2011-07-19 21:50:42 +02:00
|
|
|
Visitor *v;
|
|
|
|
|
|
|
|
v = qmp_output_get_visitor(mo);
|
2015-05-14 14:51:01 +02:00
|
|
|
visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
2011-07-19 21:50:42 +02:00
|
|
|
}
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
*ret_out = qmp_output_get_qobject(mo);
|
|
|
|
|
|
|
|
out:
|
|
|
|
error_propagate(errp, local_err);
|
2011-07-19 21:50:42 +02:00
|
|
|
qmp_output_visitor_cleanup(mo);
|
2014-05-07 09:53:44 +02:00
|
|
|
md = qapi_dealloc_visitor_new();
|
2011-07-19 21:50:42 +02:00
|
|
|
v = qapi_dealloc_get_visitor(md);
|
2015-05-14 14:51:01 +02:00
|
|
|
visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
|
2011-07-19 21:50:42 +02:00
|
|
|
qapi_dealloc_visitor_cleanup(md);
|
|
|
|
}
|
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
c_ret_type=c_type(ret_type), c_name=c_name(name),
|
2015-05-14 14:51:01 +02:00
|
|
|
visitor=type_name(ret_type))
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
def gen_marshal_input_decl(name, middle_mode):
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 17:25:50 +01:00
|
|
|
ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
|
|
|
|
if not middle_mode:
|
|
|
|
ret = "static " + ret
|
|
|
|
return ret
|
2011-09-02 19:34:46 +02:00
|
|
|
|
|
|
|
def gen_marshal_input(name, args, ret_type, middle_mode):
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
hdr = gen_marshal_input_decl(name, middle_mode)
|
2011-09-02 19:34:46 +02:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
ret = mcgen('''
|
2011-09-02 19:34:46 +02:00
|
|
|
%(header)s
|
2011-07-19 21:50:42 +02:00
|
|
|
{
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
Error *local_err = NULL;
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2011-09-02 19:34:46 +02:00
|
|
|
header=hdr)
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
if ret_type:
|
|
|
|
ret += mcgen('''
|
2015-07-31 18:51:18 +02:00
|
|
|
%(c_type)s retval;
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-07-31 18:51:18 +02:00
|
|
|
c_type=c_type(ret_type))
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
if len(args) > 0:
|
2015-06-27 17:49:34 +02:00
|
|
|
ret += gen_visitor_input_containers_decl(args)
|
|
|
|
ret += gen_visitor_input_vars_decl(args) + '\n'
|
|
|
|
ret += gen_visitor_input_block(args) + '\n'
|
2011-09-02 19:34:46 +02:00
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
(void)args;
|
2015-07-01 16:55:15 +02:00
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-06-27 17:49:34 +02:00
|
|
|
ret += gen_sync_call(name, args, ret_type)
|
|
|
|
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 09:53:54 +02:00
|
|
|
if re.search('^ *goto out\\;', ret, re.MULTILINE):
|
|
|
|
ret += mcgen('''
|
2011-07-19 21:50:42 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
''')
|
|
|
|
ret += mcgen('''
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 17:25:50 +01:00
|
|
|
error_propagate(errp, local_err);
|
2015-06-27 17:49:34 +02:00
|
|
|
''')
|
|
|
|
ret += gen_visitor_input_block(args, dealloc=True)
|
|
|
|
ret += mcgen('''
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 17:25:50 +01:00
|
|
|
}
|
2015-06-27 17:49:34 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def gen_registry(commands):
|
|
|
|
registry=""
|
|
|
|
push_indent()
|
|
|
|
for cmd in commands:
|
2012-05-08 19:24:44 +02:00
|
|
|
options = 'QCO_NO_OPTIONS'
|
qapi: Unify type bypass and add tests
For a few QMP commands, we are forced to pass an arbitrary type
without tracking it properly in QAPI. Among the existing clients,
this unnamed type was spelled 'dict', 'visitor', and '**'; this
patch standardizes on '**', matching the documentation changes
earlier in the series.
Meanwhile, for the 'gen' key, we have been ignoring the value,
although the schema consistently used "'no'" ('success-response'
was hard-coded to checking for 'no'). But now that we can support
a literal "false" in the schema, we might as well use that rather
than ignoring the value or special-casing a random string. Note
that these are one-way switches (use of 'gen':true is not the same
as omitting 'gen'). Also, the use of '**' requires 'gen':false,
but the use of 'gen':false does not mandate the use of '**'.
There is no difference to the generated code. Add some tests on
what we'd like to guarantee, although it will take later patches
to clean up test results and actually enforce the use of a bool
parameter.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-05-04 17:05:19 +02:00
|
|
|
if not cmd.get('success-response', True):
|
2012-05-08 19:24:44 +02:00
|
|
|
options = 'QCO_NO_SUCCESS_RESP'
|
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
registry += mcgen('''
|
2012-05-08 19:24:44 +02:00
|
|
|
qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
|
2011-07-19 21:50:42 +02:00
|
|
|
''',
|
2015-05-14 14:50:48 +02:00
|
|
|
name=cmd['command'], c_name=c_name(cmd['command']),
|
2012-05-08 19:24:44 +02:00
|
|
|
opts=options)
|
2011-07-19 21:50:42 +02:00
|
|
|
pop_indent()
|
|
|
|
ret = mcgen('''
|
|
|
|
static void qmp_init_marshal(void)
|
|
|
|
{
|
2015-06-27 17:49:34 +02:00
|
|
|
''')
|
|
|
|
ret += registry
|
|
|
|
ret += mcgen('''
|
2011-07-19 21:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qapi_init(qmp_init_marshal);
|
2015-06-27 17:49:34 +02:00
|
|
|
''')
|
2011-07-19 21:50:42 +02:00
|
|
|
return ret
|
|
|
|
|
2011-09-02 19:34:46 +02:00
|
|
|
middle_mode = False
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-04-02 13:12:21 +02:00
|
|
|
(input_file, output_dir, do_c, do_h, prefix, opts) = \
|
|
|
|
parse_command_line("m", ["middle"])
|
2011-12-27 15:02:16 +01:00
|
|
|
|
2011-07-19 21:50:42 +02:00
|
|
|
for o, a in opts:
|
2015-04-02 13:12:21 +02:00
|
|
|
if o in ("-m", "--middle"):
|
2011-09-02 19:34:46 +02:00
|
|
|
middle_mode = True
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2014-05-02 15:52:35 +02:00
|
|
|
exprs = parse_schema(input_file)
|
2011-07-19 21:50:42 +02:00
|
|
|
commands = filter(lambda expr: expr.has_key('command'), exprs)
|
2011-12-12 21:29:33 +01:00
|
|
|
commands = filter(lambda expr: not expr.has_key('gen'), commands)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-04-02 14:46:39 +02:00
|
|
|
c_comment = '''
|
|
|
|
/*
|
|
|
|
* schema-defined QMP->QAPI command dispatch
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
h_comment = '''
|
|
|
|
/*
|
|
|
|
* schema-defined QAPI function prototypes
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
|
|
|
|
(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
|
|
|
|
'qmp-marshal.c', 'qmp-commands.h',
|
|
|
|
c_comment, h_comment)
|
|
|
|
|
2015-04-02 14:52:55 +02:00
|
|
|
fdef.write(mcgen('''
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu/module.h"
|
|
|
|
#include "qapi/qmp/types.h"
|
|
|
|
#include "qapi/qmp/dispatch.h"
|
|
|
|
#include "qapi/visitor.h"
|
|
|
|
#include "qapi/qmp-output-visitor.h"
|
|
|
|
#include "qapi/qmp-input-visitor.h"
|
|
|
|
#include "qapi/dealloc-visitor.h"
|
|
|
|
#include "%(prefix)sqapi-types.h"
|
|
|
|
#include "%(prefix)sqapi-visit.h"
|
|
|
|
#include "%(prefix)sqmp-commands.h"
|
|
|
|
|
|
|
|
''',
|
|
|
|
prefix=prefix))
|
|
|
|
|
|
|
|
fdecl.write(mcgen('''
|
|
|
|
#include "%(prefix)sqapi-types.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
|
|
|
|
''',
|
|
|
|
prefix=prefix))
|
2015-04-02 11:41:22 +02:00
|
|
|
|
|
|
|
for cmd in commands:
|
|
|
|
arglist = []
|
|
|
|
ret_type = None
|
|
|
|
if cmd.has_key('data'):
|
|
|
|
arglist = cmd['data']
|
|
|
|
if cmd.has_key('returns'):
|
|
|
|
ret_type = cmd['returns']
|
2015-06-27 17:49:34 +02:00
|
|
|
ret = generate_command_decl(cmd['command'], arglist, ret_type)
|
2011-07-19 21:50:42 +02:00
|
|
|
fdecl.write(ret)
|
2015-04-02 11:41:22 +02:00
|
|
|
if ret_type:
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
|
2011-07-19 21:50:42 +02:00
|
|
|
fdef.write(ret)
|
|
|
|
|
2015-04-02 11:41:22 +02:00
|
|
|
if middle_mode:
|
qapi: Drop unused and useless parameters and variables
gen_sync_call()'s parameter indent is useless: gen_sync_call() uses it
only as optional argument for push_indent() and pop_indent(), their
default is four, and gen_sync_call()'s only caller passes four. Drop
the parameter.
gen_visitor_input_containers_decl()'s parameter obj is always
"QOBJECT(args)". Use that, and drop the parameter.
Drop unused parameters of gen_marshal_output(),
gen_marshal_input_decl(), generate_visit_struct_body(),
generate_visit_list(), generate_visit_enum(), generate_declaration(),
generate_enum_declaration(), generate_decl_enum().
Drop unused variables in generate_event_enum_lookup(),
generate_enum_lookup(), generate_visit_struct_fields(), check_event().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-06-28 21:36:26 +02:00
|
|
|
fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
|
2011-09-02 19:34:46 +02:00
|
|
|
|
2015-04-02 11:41:22 +02:00
|
|
|
ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
|
|
|
|
fdef.write(ret)
|
|
|
|
|
|
|
|
if not middle_mode:
|
|
|
|
ret = gen_registry(commands)
|
|
|
|
fdef.write(ret)
|
2011-07-19 21:50:42 +02:00
|
|
|
|
2015-04-02 14:46:39 +02:00
|
|
|
close_output(fdef, fdecl)
|