X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fstop.c;h=5ac182b326e9cedee09c1341e130f706a41862c3;hb=6c1c0768320135c6936c371b09731851b508c023;hp=60a1dac8fe8ca4db4fd58c19d7d4a904012acd70;hpb=38ee087f699718e57d1bc5614c2f79c3c30ccca9;p=lttng-tools.git diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c index 60a1dac8f..5ac182b32 100644 --- a/src/bin/lttng/commands/stop.c +++ b/src/bin/lttng/commands/stop.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -23,13 +24,16 @@ #include #include #include - -#include "../command.h" +#include #include +#include + +#include "../command.h" static char *opt_session_name; static int opt_no_wait; +static struct mi_writer *writer; enum { OPT_HELP = 1, @@ -60,6 +64,41 @@ static void usage(FILE *ofp) fprintf(ofp, " -n, --no-wait Don't wait for data availability\n"); fprintf(ofp, "\n"); } +/* + * Mi print of partial session + */ +static int mi_print_session(char *session_name, int enabled) +{ + int ret; + assert(writer); + assert(session_name); + + /* Open session element */ + ret = mi_lttng_writer_open_element(writer, config_element_session); + if (ret) { + goto end; + } + + /* Print session name element */ + ret = mi_lttng_writer_write_element_string(writer, config_element_name, + session_name); + if (ret) { + goto end; + } + + /* Is enabled ? */ + ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, + enabled); + if (ret) { + goto end; + } + + /* Close session element */ + ret = mi_lttng_writer_close_element(writer); + +end: + return ret; +} /* * Start tracing for all trace of the session. @@ -79,11 +118,7 @@ static int stop_tracing(void) session_name = opt_session_name; } - if (opt_no_wait) { - ret = lttng_stop_tracing_no_wait(session_name); - } else { - ret = lttng_stop_tracing(session_name); - } + ret = lttng_stop_tracing_no_wait(session_name); if (ret < 0) { switch (-ret) { case LTTNG_ERR_TRACE_ALREADY_STOPPED: @@ -96,9 +131,38 @@ static int stop_tracing(void) goto free_name; } + if (!opt_no_wait) { + _MSG("Waiting for data availability"); + fflush(stdout); + do { + ret = lttng_data_pending(session_name); + if (ret < 0) { + /* Return the data available call error. */ + goto free_name; + } + + /* + * Data sleep time before retrying (in usec). Don't sleep if the call + * returned value indicates availability. + */ + if (ret) { + usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); + _MSG("."); + fflush(stdout); + } + } while (ret != 0); + MSG(""); + } + ret = CMD_SUCCESS; MSG("Tracing stopped for session %s", session_name); + if (lttng_opt_mi) { + ret = mi_print_session(session_name, 0); + if (ret) { + goto free_name; + } + } free_name: if (opt_session_name == NULL) { @@ -116,7 +180,7 @@ error: */ int cmd_stop(int argc, const char **argv) { - int opt, ret = CMD_SUCCESS; + int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; static poptContext pc; pc = poptGetContext(NULL, argc, argv, long_options, 0); @@ -137,11 +201,84 @@ int cmd_stop(int argc, const char **argv) } } + /* 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_stop); + 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; + } + + /* + * Open sessions element + * For validation + */ + ret = mi_lttng_writer_open_element(writer, + config_element_sessions); + if (ret) { + ret = CMD_ERROR; + goto end; + } + } + opt_session_name = (char*) poptGetArg(pc); - ret = stop_tracing(); + command_ret = stop_tracing(); + if (command_ret) { + success = 0; + } + + /* Mi closing */ + if (lttng_opt_mi) { + /* Close sessions and output element */ + ret = mi_lttng_close_multi_element(writer, 2); + 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: + /* 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 an error occurred in stop_tracing() */ + ret = command_ret ? command_ret : ret; + poptFreeContext(pc); return ret; }