From: Jérémie Galarneau Date: Mon, 29 Feb 2016 22:52:58 +0000 (-0500) Subject: Add schema information to XML MI output X-Git-Tag: v2.8.0-rc1~134 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=e10b6a1c2fad4f300523cf8cfb57aba2bfa101c0 Add schema information to XML MI output Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/config/session-config.c b/src/common/config/session-config.c index 11e0dcc4e..dec891349 100644 --- a/src/common/config/session-config.c +++ b/src/common/config/session-config.c @@ -458,6 +458,39 @@ end: return ret >= 0 ? 0 : ret; } +LTTNG_HIDDEN +int config_writer_write_attribute(struct config_writer *writer, + const char *name, const char *value) +{ + int ret; + xmlChar *encoded_name = NULL; + xmlChar *encoded_value = NULL; + + if (!writer || !writer->writer || !name || !name[0]) { + ret = -1; + goto end; + } + + encoded_name = encode_string(name); + if (!encoded_name) { + ret = -1; + goto end; + } + + encoded_value = encode_string(value); + if (!encoded_value) { + ret = -1; + goto end; + } + + ret = xmlTextWriterWriteAttribute(writer->writer, encoded_name, + encoded_value); +end: + xmlFree(encoded_name); + xmlFree(encoded_value); + return ret >= 0 ? 0 : ret; +} + LTTNG_HIDDEN int config_writer_close_element(struct config_writer *writer) { diff --git a/src/common/config/session-config.h b/src/common/config/session-config.h index 2fed612a1..a53a8f91c 100644 --- a/src/common/config/session-config.h +++ b/src/common/config/session-config.h @@ -108,13 +108,27 @@ int config_writer_destroy(struct config_writer *writer); * * element_name Element tag name. * - * Returns zero if the XML document could be closed cleanly. + * Returns zero if the XML element could be opened. * Negative values indicate an error. */ LTTNG_HIDDEN int config_writer_open_element(struct config_writer *writer, const char *element_name); +/* + * Write an element tag attribute. + * + * writer An instance of a configuration writer. + * + * name Attribute name. + * + * Returns zero if the XML element's attribute could be written. + * Negative values indicate an error. + */ +LTTNG_HIDDEN +int config_writer_write_attribute(struct config_writer *writer, + const char *name, const char *value); + /* * Close the current element tag. * diff --git a/src/common/mi-lttng.c b/src/common/mi-lttng.c index 30bd60328..f77b6b648 100644 --- a/src/common/mi-lttng.c +++ b/src/common/mi-lttng.c @@ -1,6 +1,7 @@ /* * Copyright (C) 2014 - Jonathan Rajotte * - Olivier Cotte + * Copyright (C) 2016 - Jérémie Galarneau * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2 only, as @@ -23,6 +24,24 @@ #include +#define MI_SCHEMA_MAJOR_VERSION 3 +#define MI_SCHEMA_MINOR_VERSION 0 + +/* Machine interface namespace URI */ +const char * const mi_lttng_xmlns = "xmlns"; +const char * const mi_lttng_ns_uri = "http://lttng.org/xml/ns/lttng-mi"; +const char * const mi_lttng_xmlns_xsi = "xmlns:xsi"; +const char * const mi_lttng_w3_schema_uri = "http://www.w3.org/2001/XMLSchema-instance"; +const char * const mi_lttng_schema_location = "xsi:schemaLocation"; +const char * const mi_lttng_schema_location_uri = + "http://lttng.org/xml/ns/lttng-mi" " " + "http://lttng.org/xml/schemas/lttng-mi/" XSTR(MI_SCHEMA_MAJOR_VERSION) + "/lttng-mi-" XSTR(MI_SCHEMA_MAJOR_VERSION) "." + XSTR(MI_SCHEMA_MINOR_VERSION) ".xsd"; +const char * const mi_lttng_schema_version = "schemaVersion"; +const char * const mi_lttng_schema_version_value = XSTR(MI_SCHEMA_MAJOR_VERSION) + "." XSTR(MI_SCHEMA_MINOR_VERSION); + /* Strings related to command */ const char * const mi_lttng_element_command = "command"; const char * const mi_lttng_element_command_action = "snapshot_action"; @@ -487,10 +506,42 @@ int mi_lttng_writer_command_open(struct mi_writer *writer, const char *command) { int ret; - ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command); + /* + * A command is always the MI's root node, it must declare the current + * namespace and schema URIs and the schema's version. + */ + ret = config_writer_open_element(writer->writer, + mi_lttng_element_command); + if (ret) { + goto end; + } + + ret = config_writer_write_attribute(writer->writer, + mi_lttng_xmlns, mi_lttng_ns_uri); + if (ret) { + goto end; + } + + ret = config_writer_write_attribute(writer->writer, + mi_lttng_xmlns_xsi, mi_lttng_w3_schema_uri); + if (ret) { + goto end; + } + + ret = config_writer_write_attribute(writer->writer, + mi_lttng_schema_location, + mi_lttng_schema_location_uri); if (ret) { goto end; } + + ret = config_writer_write_attribute(writer->writer, + mi_lttng_schema_version, + mi_lttng_schema_version_value); + if (ret) { + goto end; + } + ret = mi_lttng_writer_write_element_string(writer, mi_lttng_element_command_name, command); end: