From: Jonathan Rajotte Julien Date: Tue, 17 Jun 2014 17:00:38 +0000 (-0400) Subject: Mi save&load command: support and validation X-Git-Tag: v2.6.0-rc1~128 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=1734c658bc988376ddf880872e9d37160edea133;p=lttng-tools.git Mi save&load command: support and validation Signed-off-by: Jonathan Rajotte Julien --- diff --git a/src/bin/lttng/commands/load.c b/src/bin/lttng/commands/load.c index 347e4ca08..150b880c0 100644 --- a/src/bin/lttng/commands/load.c +++ b/src/bin/lttng/commands/load.c @@ -23,7 +23,9 @@ #include #include +#include #include + #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 ' 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; } diff --git a/src/bin/lttng/commands/save.c b/src/bin/lttng/commands/save.c index fdc0da6bf..d90ae0634 100644 --- a/src/bin/lttng/commands/save.c +++ b/src/bin/lttng/commands/save.c @@ -23,6 +23,8 @@ #include #include +#include + #include "../command.h" #include @@ -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 ' 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; } diff --git a/src/common/mi-lttng.c b/src/common/mi-lttng.c index 7364c5d82..12f7030fa 100644 --- a/src/common/mi-lttng.c +++ b/src/common/mi-lttng.c @@ -27,10 +27,13 @@ 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"; diff --git a/src/common/mi-lttng.h b/src/common/mi-lttng.h index e2b5de71c..a7b3006fa 100644 --- a/src/common/mi-lttng.h +++ b/src/common/mi-lttng.h @@ -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; diff --git a/src/common/mi_lttng.xsd b/src/common/mi_lttng.xsd index d41b1eda1..8aca79f40 100644 --- a/src/common/mi_lttng.xsd +++ b/src/common/mi_lttng.xsd @@ -258,10 +258,10 @@ THE SOFTWARE. - - - - + + + + @@ -275,6 +275,21 @@ THE SOFTWARE. + + + + + + + + + + + + + + + @@ -299,6 +314,8 @@ THE SOFTWARE. + + @@ -307,6 +324,8 @@ THE SOFTWARE. + + @@ -315,6 +334,7 @@ THE SOFTWARE. +