Mi save&load command: support and validation
authorJonathan Rajotte Julien <jonathan.r.julien@gmail.com>
Tue, 17 Jun 2014 17:00:38 +0000 (13:00 -0400)
committerJonathan Rajotte Julien <jonathan.r.julien@gmail.com>
Tue, 22 Jul 2014 20:14:56 +0000 (16:14 -0400)
Signed-off-by: Jonathan Rajotte Julien <jonathan.r.julien@gmail.com>
src/bin/lttng/commands/load.c
src/bin/lttng/commands/save.c
src/common/mi-lttng.c
src/common/mi-lttng.h
src/common/mi_lttng.xsd

index 347e4ca08896b8507a9e0939c281a0046a946bec..150b880c0e833b839ee60b1e0abaeda723677090 100644 (file)
@@ -23,7 +23,9 @@
 #include <string.h>
 #include <assert.h>
 
+#include <common/mi-lttng.h>
 #include <common/config/config.h>
+
 #include "../command.h"
 
 static char *opt_input_path;
@@ -38,6 +40,8 @@ enum {
        OPT_FORCE,
 };
 
+static struct mi_writer *writer;
+
 static struct poptOption load_opts[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
        {"help",        'h',  POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
@@ -65,25 +69,83 @@ static void usage(FILE *ofp)
        fprintf(ofp, "                           before creating new one(s).\n");
 }
 
+static int mi_partial_session(const char *session_name)
+{
+       int ret;
+       assert(writer);
+       assert(session_name);
+
+       /* Open session element */
+       ret = mi_lttng_writer_open_element(writer, config_element_session);
+       if (ret) {
+               goto end;
+       }
+
+       ret = mi_lttng_writer_write_element_string(writer, config_element_name,
+                       session_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Closing session element */
+       ret = mi_lttng_writer_close_element(writer);
+end:
+       return ret;
+}
+
+/*
+ * Mi print of load command
+ */
+static int mi_load_print(const char *session_name)
+{
+       int ret;
+       assert(writer);
+
+       if (opt_load_all) {
+               /* We use a wildcard to represent all sessions */
+               session_name = "*";
+       }
+
+       /* Print load element */
+       ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
+       if (ret) {
+               goto end;
+       }
+
+       /* Print session element */
+       ret = mi_partial_session(session_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Path element */
+       if (opt_input_path) {
+               ret = mi_lttng_writer_write_element_string(writer, config_element_path,
+                               opt_input_path);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* Close load element */
+       ret = mi_lttng_writer_close_element(writer);
+
+end:
+       return ret;
+}
+
 /*
  * The 'load <options>' first level command
  */
 int cmd_load(int argc, const char **argv)
 {
-       int ret = CMD_SUCCESS;
+       int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
        int opt;
        poptContext pc;
 
        pc = poptGetContext(NULL, argc, argv, load_opts, 0);
        poptReadDefaultConfig(pc, 0);
 
-       /* TODO: mi support */
-       if (lttng_opt_mi) {
-               ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
-               ERR("mi option not supported");
-               goto end;
-       }
-
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
@@ -106,13 +168,41 @@ int cmd_load(int argc, const char **argv)
                session_name = poptGetArg(pc);
                if (session_name) {
                        DBG2("Loading session name: %s", session_name);
+               } else {
+                       /* Default to load_all */
+                       opt_load_all = 1;
                }
        }
 
-       ret = config_load_session(opt_input_path, session_name, opt_force, 0);
-       if (ret) {
-               ERR("%s", lttng_strerror(ret));
-               ret = -ret;
+       /* Mi check */
+       if (lttng_opt_mi) {
+               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
+               if (!writer) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               /* Open command element */
+               ret = mi_lttng_writer_command_open(writer,
+                               mi_lttng_element_command_load);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Open output element */
+               ret = mi_lttng_writer_open_element(writer,
+                               mi_lttng_element_command_output);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
+       command_ret = config_load_session(opt_input_path, session_name, opt_force, 0);
+       if (command_ret) {
+               ERR("%s", lttng_strerror(command_ret));
+               success = 0;
        } else {
                if (opt_load_all) {
                        MSG("All sessions have been loaded successfully");
@@ -125,8 +215,49 @@ int cmd_load(int argc, const char **argv)
                } else {
                        MSG("Session has been loaded successfully");
                }
+               success = 1;
+       }
+
+       /* Mi Printing and closing */
+       if (lttng_opt_mi) {
+               /* Mi print */
+               ret = mi_load_print(session_name);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Close  output element */
+               ret = mi_lttng_writer_close_element(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Success ? */
+               ret = mi_lttng_writer_write_element_bool(writer,
+                               mi_lttng_element_command_success, success);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Command element close */
+               ret = mi_lttng_writer_command_close(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
        }
 end:
+       if (writer && mi_lttng_writer_destroy(writer)) {
+               /* Preserve original error code */
+               ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
+       }
+
+       /* Overwrite ret if the was an error with the load command */
+       ret = command_ret ? -command_ret : ret;
+
        poptFreeContext(pc);
        return ret;
 }
index fdc0da6bf2ee921b6438fef2df6676ed88f5ad20..d90ae06344179d8013e398c59a2a4c57e00049d5 100644 (file)
@@ -23,6 +23,8 @@
 #include <string.h>
 #include <assert.h>
 
+#include <common/mi-lttng.h>
+
 #include "../command.h"
 #include <lttng/save.h>
 
@@ -45,6 +47,8 @@ static struct poptOption save_opts[] = {
        {0, 0, 0, 0, 0, 0, 0}
 };
 
+static struct mi_writer *writer;
+
 /*
  * usage
  */
@@ -59,12 +63,76 @@ static void usage(FILE *ofp)
        fprintf(ofp, "  -f, --force          Overwrite existing session configuration(s)\n");
 }
 
+static int mi_partial_session(const char *session_name)
+{
+       int ret;
+       assert(writer);
+       assert(session_name);
+
+       /* Open session element */
+       ret = mi_lttng_writer_open_element(writer, config_element_session);
+       if (ret) {
+               goto end;
+       }
+
+       ret = mi_lttng_writer_write_element_string(writer, config_element_name,
+                       session_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Closing session element */
+       ret = mi_lttng_writer_close_element(writer);
+end:
+       return ret;
+}
+
+/*
+ * Mi print of save command
+ */
+static int mi_save_print(const char *session_name)
+{
+       int ret;
+       assert(writer);
+
+       if (opt_save_all) {
+               /* We use a wildcard to represent all sessions */
+               session_name = "*";
+       }
+
+       /* Print save element */
+       ret = mi_lttng_writer_open_element(writer, mi_lttng_element_save);
+       if (ret) {
+               goto end;
+       }
+
+       /* Print session element */
+       ret = mi_partial_session(session_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Path element */
+       if (opt_output_path) {
+               ret = mi_lttng_writer_write_element_string(writer, config_element_path,
+                               opt_output_path);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* Close save element */
+       ret = mi_lttng_writer_close_element(writer);
+end:
+       return ret;
+}
+
 /*
  * The 'save <options>' first level command
  */
 int cmd_save(int argc, const char **argv)
 {
-       int ret = CMD_SUCCESS;
+       int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
        int opt;
        const char *session_name = NULL;
        poptContext pc;
@@ -73,13 +141,6 @@ int cmd_save(int argc, const char **argv)
        pc = poptGetContext(NULL, argc, argv, save_opts, 0);
        poptReadDefaultConfig(pc, 0);
 
-       /* TODO: mi support */
-       if (lttng_opt_mi) {
-               ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
-               ERR("mi option not supported");
-               goto end;
-       }
-
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
@@ -102,6 +163,9 @@ int cmd_save(int argc, const char **argv)
                session_name = poptGetArg(pc);
                if (session_name) {
                        DBG2("Session name: %s", session_name);
+               } else {
+                       /* default to opt_save_all */
+                       opt_save_all = 1;
                }
        }
 
@@ -126,9 +190,35 @@ int cmd_save(int argc, const char **argv)
                goto end_destroy;
        }
 
-       ret = lttng_save_session(attr);
-       if (ret < 0) {
-               ERR("%s", lttng_strerror(ret));
+       /* Mi check */
+       if (lttng_opt_mi) {
+               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
+               if (!writer) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end_destroy;
+               }
+
+               /* Open command element */
+               ret = mi_lttng_writer_command_open(writer,
+                               mi_lttng_element_command_save);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
+
+               /* Open output element */
+               ret = mi_lttng_writer_open_element(writer,
+                               mi_lttng_element_command_output);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
+       }
+
+       command_ret = lttng_save_session(attr);
+       if (command_ret < 0) {
+               ERR("%s", lttng_strerror(command_ret));
+               success = 0;
        } else {
                /* Inform the user of what just happened on success. */
                if (session_name && opt_output_path) {
@@ -142,10 +232,52 @@ int cmd_save(int argc, const char **argv)
                } else {
                        MSG("All sessions have been saved successfully.");
                }
+               success = 1;
+       }
+
+       /* Mi Printing and closing */
+       if (lttng_opt_mi) {
+               /* Mi print */
+               ret = mi_save_print(session_name);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
+
+               /* Close  output element */
+               ret = mi_lttng_writer_close_element(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
+
+               /* Success ? */
+               ret = mi_lttng_writer_write_element_bool(writer,
+                               mi_lttng_element_command_success, success);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
+
+               /* Command element close */
+               ret = mi_lttng_writer_command_close(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end_destroy;
+               }
        }
 end_destroy:
        lttng_save_session_attr_destroy(attr);
 end:
+       /* Mi clean-up */
+       if (writer && mi_lttng_writer_destroy(writer)) {
+               /* Preserve original error code */
+               ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
+       }
+
+       /* Overwrite ret if command failed */
+       ret = command_ret ? -command_ret : ret;
+
        poptFreeContext(pc);
        return ret;
 }
index 7364c5d82ce8a6a9b85acf9ee771a0c6210ff488..12f7030fa6e11f37b7e7ee276fd79f264951eeab 100644 (file)
 const char * const mi_lttng_element_command = "command";
 const char * const mi_lttng_element_command_version = "version";
 const char * const mi_lttng_element_command_list = "list";
+const char * const mi_lttng_element_command_save = "save";
+const char * const mi_lttng_element_command_load = "load";
 const char * const mi_lttng_element_command_name = "name";
 const char * const mi_lttng_element_command_output = "output";
+const char * const mi_lttng_element_command_success = "success";
 
-/* Strings related to command: version */
+/* Strings related to version command */
 const char * const mi_lttng_element_version = "version";
 const char * const mi_lttng_element_version_str = "string";
 const char * const mi_lttng_element_version_web = "url";
@@ -46,6 +49,12 @@ const char * const mi_lttng_element_pids = "pids";
 const char * const mi_lttng_element_pid = "pid";
 const char * const mi_lttng_element_pid_id = "id";
 
+/* Strings related to save command */
+const char * const mi_lttng_element_save = "save";
+
+/* Strings related to load command */
+const char * const mi_lttng_element_load = "load";
+
 /* String related to a lttng_event_field */
 const char * const mi_lttng_element_event_field = "event_field";
 const char * const mi_lttng_element_event_fields = "event_fields";
@@ -76,6 +85,7 @@ const char * const mi_lttng_loglevel_str_notice = "TRACE_NOTICE";
 const char * const mi_lttng_loglevel_str_unknown = "UNKNOWN";
 const char * const mi_lttng_loglevel_str_warning = "TRACE_WARNING";
 
+/* String related to loglevel type */
 const char * const mi_lttng_loglevel_type_all = "ALL";
 const char * const mi_lttng_loglevel_type_range = "RANGE";
 const char * const mi_lttng_loglevel_type_single = "SINGLE";
index e2b5de71c10d14f88ec41270371475b97ec1bc5e..a7b3006fa79dd4f54be9b9c458f3e85e32d79d7f 100644 (file)
@@ -49,10 +49,13 @@ struct mi_lttng_version {
 const char * const mi_lttng_element_command;
 const char * const mi_lttng_element_command_version;
 const char * const mi_lttng_element_command_list;
+const char * const mi_lttng_element_command_save;
+const char * const mi_lttng_element_command_load;
 const char * const mi_lttng_element_command_name;
 const char * const mi_lttng_element_command_output;
+const char * const mi_lttng_element_command_success;
 
-/* Strings related to command: version */
+/* Strings related to version command */
 const char * const mi_lttng_element_version;
 const char * const mi_lttng_element_version_str;
 const char * const mi_lttng_element_version_web;
@@ -72,6 +75,12 @@ const char * const mi_lttng_element_pids;
 const char * const mi_lttng_element_pid;
 const char * const mi_lttng_element_pid_id;
 
+/* Strings related to save command */
+const char * const mi_lttng_element_save;
+
+/* Strings related to load command */
+const char * const mi_lttng_element_load;
+
 /* General element of mi_lttng */
 const char * const mi_lttng_element_type_other;
 const char * const mi_lttng_element_type_integer;
@@ -98,6 +107,7 @@ const char * const mi_lttng_loglevel_str_notice;
 const char * const mi_lttng_loglevel_str_unknown;
 const char * const mi_lttng_loglevel_str_warning;
 
+/* String related to loglevel type */
 const char * const mi_lttng_loglevel_type_all;
 const char * const mi_lttng_loglevel_type_range;
 const char * const mi_lttng_loglevel_type_single;
index d41b1eda1f75574ab3a4f8d9bcf6ae35f3241b43..8aca79f40cd19864f3fd6d6297872d3ca0770f3f 100644 (file)
@@ -258,10 +258,10 @@ THE SOFTWARE.
        <xs:complexType name="session_type">
                <xs:all>
                        <xs:element name="name" type="name_type" />
-                       <xs:element name="path" type="name_type" />
-                       <xs:element name="enabled" type="xs:boolean" default="false" />
-                       <xs:element name="snapshot_mode" type="uint32_type" />
-                       <xs:element name="live_timer_interval" type="uint32_type" />
+                       <xs:element name="path" type="name_type" minOccurs="0" />
+                       <xs:element name="enabled" type="xs:boolean" default="false" minOccurs="0" />
+                       <xs:element name="snapshot_mode" type="uint32_type" minOccurs="0" />
+                       <xs:element name="live_timer_interval" type="uint32_type" minOccurs="0" />
                        <xs:element name="channels" type="channels_type" minOccurs="0" />
                        <xs:element name="domains" type="domains_type" minOccurs="0" />
                </xs:all>
@@ -275,6 +275,21 @@ THE SOFTWARE.
                </xs:all>
        </xs:complexType>
 
+       <!-- map to the save command -->
+       <xs:complexType name="save_type">
+               <xs:all>
+                       <xs:element name="session" type="session_type" />
+                       <xs:element name="path" type="name_type"/>
+               </xs:all>
+       </xs:complexType>
+
+       <!-- map to the load command -->
+       <xs:complexType name="load_type">
+               <xs:all>
+                       <xs:element name="session" type="session_type" />
+                       <xs:element name="path" type="name_type"/>
+               </xs:all>
+       </xs:complexType>
 
        <xs:complexType name="domains_type">
                <xs:sequence>
@@ -299,6 +314,8 @@ THE SOFTWARE.
                        <xs:element name="domains" type="domains_type" minOccurs="0" />
                        <xs:element name="sessions" type="sessions_type" minOccurs="0" />
                        <xs:element name="version" type="version_type" minOccurs="0" />
+                       <xs:element name="save" type="save_type" minOccurs="0" />
+                       <xs:element name="load" type="load_type" minOccurs="0" />
                </xs:choice>
        </xs:complexType>
 
@@ -307,6 +324,8 @@ THE SOFTWARE.
                <xs:restriction base="xs:string">
                        <xs:enumeration value="list" />
                        <xs:enumeration value="version" />
+                       <xs:enumeration value="save" />
+                       <xs:enumeration value="load" />
                </xs:restriction>
        </xs:simpleType>
 
@@ -315,6 +334,7 @@ THE SOFTWARE.
                        <xs:all>
                                <xs:element name="name" type="command_string_type" maxOccurs="1" />
                                <xs:element name="output" type="output_type" maxOccurs="1" />
+                               <xs:element name="success" type="xs:boolean" minOccurs="0" maxOccurs="1" />
                        </xs:all>
                </xs:complexType>
        </xs:element>
This page took 0.032828 seconds and 4 git commands to generate.