* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#define _GNU_SOURCE
+#define _LGPL_SOURCE
#include <ctype.h>
#include <popt.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
+#include <assert.h>
#include <urcu/list.h>
+#include <common/mi-lttng.h>
+
#include "../command.h"
#define PRINT_LINE_LEN 80
static int opt_kernel;
static int opt_userspace;
static char *opt_type;
-#if 0
-/* Not implemented yet */
-static char *opt_cmd_name;
-static pid_t opt_pid;
-#endif
enum {
OPT_HELP = 1,
};
static struct lttng_handle *handle;
+static struct mi_writer *writer;
/*
* Taken from the LTTng ABI
*/
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;
} else if (opt_userspace) {
dom.type = LTTNG_DOMAIN_UST;
} else {
- print_missing_domain();
- ret = CMD_ERROR;
- goto error;
+ assert(0);
}
handle = lttng_create_handle(session_name, &dom);
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;
}
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",
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;
}
}
* 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;
*/
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;
goto end;
}
- type = malloc(sizeof(struct ctx_type));
+ type = zmalloc(sizeof(struct ctx_type));
if (type == NULL) {
- perror("malloc ctx_type");
+ PERROR("malloc ctx_type");
ret = CMD_FATAL;
goto end;
}
break;
case OPT_USERSPACE:
opt_userspace = 1;
-#if 0
- opt_cmd_name = poptGetOptArg(pc);
-#endif
break;
case OPT_LIST_OPTIONS:
list_cmd_options(stdout, long_options);
}
}
+ ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
+
+ if (ret) {
+ ret = CMD_ERROR;
+ goto end;
+ }
+
if (!opt_type) {
ERR("Missing mandatory -t TYPE");
usage(stderr);
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;
}