X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fadd_context.c;h=e50ed84b6e97b9e0e7756ebf4b4a8c3b1bcaffba;hp=2f7559c3017d66d4679b7265aad97166eb65b449;hb=89b725777551ead53c60d1bd56ee7182fe539251;hpb=7e66b1b0255bbe6b5a55a83d3ff182ba3d4300ee diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c index 2f7559c30..e50ed84b6 100644 --- a/src/bin/lttng/commands/add_context.c +++ b/src/bin/lttng/commands/add_context.c @@ -27,6 +27,8 @@ #include +#include + #include "../command.h" #define PRINT_LINE_LEN 80 @@ -36,6 +38,7 @@ static char *opt_session_name; static int opt_kernel; static int opt_userspace; static char *opt_type; + #if 0 /* Not implemented yet */ static char *opt_cmd_name; @@ -50,6 +53,7 @@ enum { }; static struct lttng_handle *handle; +static struct mi_writer *writer; /* * Taken from the LTTng ABI @@ -549,7 +553,7 @@ end: */ static int add_context(char *session_name) { - int ret = CMD_SUCCESS, warn = 0; + int ret = CMD_SUCCESS, warn = 0, success = 0; struct lttng_event_context context; struct lttng_domain dom; struct ctx_type *type; @@ -574,6 +578,14 @@ static int add_context(char *session_name) goto error; } + if (lttng_opt_mi) { + /* Open a contexts element */ + ret = mi_lttng_writer_open_element(writer, config_element_contexts); + if (ret) { + goto error; + } + } + /* Iterate over all the context types given */ cds_list_for_each_entry(type, &ctx_type_list.head, list) { context.ctx = (enum lttng_event_context_type) type->opt->ctx_type; @@ -599,11 +611,20 @@ static int add_context(char *session_name) } DBG("Adding context..."); + if (lttng_opt_mi) { + /* We leave context open the update the success of the command */ + ret = mi_lttng_context(writer, &context, 1); + if (ret) { + ret = CMD_ERROR; + goto error; + } + } + ret = lttng_add_context(handle, &context, NULL, opt_channel_name); if (ret < 0) { ERR("%s: %s", type->opt->symbol, lttng_strerror(ret)); warn = 1; - continue; + success = 0; } else { if (opt_channel_name) { MSG("%s context %s added to channel %s", @@ -613,6 +634,32 @@ static int add_context(char *session_name) MSG("%s context %s added to all channels", opt_kernel ? "kernel" : "UST", type->opt->symbol) } + success = 1; + } + + if (lttng_opt_mi) { + /* Is the single operation a success ? */ + ret = mi_lttng_writer_write_element_bool(writer, + mi_lttng_element_success, success); + if (ret) { + ret = CMD_ERROR; + goto error; + } + + /* Close the context element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto error; + } + } + } + + if (lttng_opt_mi) { + /* Close contexts element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + goto error; } } @@ -625,7 +672,7 @@ error: * This means that at least one add_context failed and tells the user to * look on stderr for error(s). */ - if (warn) { + if (!ret && warn) { ret = CMD_WARNING; } return ret; @@ -636,7 +683,8 @@ error: */ int cmd_add_context(int argc, const char **argv) { - int index, opt, ret = CMD_SUCCESS; + int index, opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; + int success = 1; static poptContext pc; struct ctx_type *type, *tmptype; char *session_name = NULL; @@ -647,13 +695,6 @@ int cmd_add_context(int argc, const char **argv) goto end; } - /* TODO: mi support */ - if (lttng_opt_mi) { - ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED; - ERR("mi option not supported"); - goto end; - } - pc = poptGetContext(NULL, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); @@ -724,18 +765,80 @@ int cmd_add_context(int argc, const char **argv) session_name = opt_session_name; } - ret = add_context(session_name); + /* 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_add_context); + 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 = add_context(session_name); + if (command_ret) { + success = 0; + } + + /* Mi closing */ + if (lttng_opt_mi) { + /* 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 (!opt_session_name) { free(session_name); } -end: + /* Mi clean-up */ + if (writer && mi_lttng_writer_destroy(writer)) { + /* Preserve original error code */ + ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; + } + /* Cleanup allocated memory */ cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) { free(type); } + /* Overwrite ret if an error occurred during add_context() */ + ret = command_ret ? command_ret : ret; + poptFreeContext(pc); return ret; }