X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fstop.c;h=c38fc894a741debd87a91aec43ec69701a1af178;hp=689fe1b04998f8451d8c5018670ab5d1b25f1f65;hb=38b4ef1b199ddb15db78774a39e9c524ca7e2d24;hpb=ca1c3607d2f5654163875cda874f43971df0f696 diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c index 689fe1b04..c38fc894a 100644 --- a/src/bin/lttng/commands/stop.c +++ b/src/bin/lttng/commands/stop.c @@ -1,22 +1,21 @@ /* * Copyright (C) 2011 - David Goulet * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; only version 2 - * of the License. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -24,10 +23,16 @@ #include #include #include +#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, @@ -38,22 +43,44 @@ static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, + {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0} }; /* - * usage + * Mi print of partial session */ -static void usage(FILE *ofp) +static int mi_print_session(char *session_name, int enabled) { - fprintf(ofp, "usage: lttng stop [options] [NAME]\n"); - fprintf(ofp, "\n"); - fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); - fprintf(ofp, "get it from the configuration directory (.lttng).\n"); - fprintf(ofp, "\n"); - fprintf(ofp, " -h, --help Show this help\n"); - fprintf(ofp, " --list-options Simple listing of options\n"); - fprintf(ofp, "\n"); + 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; } /* @@ -61,7 +88,7 @@ static void usage(FILE *ofp) */ static int stop_tracing(void) { - int ret = CMD_SUCCESS; + int ret; char *session_name; if (opt_session_name == NULL) { @@ -74,12 +101,52 @@ static int stop_tracing(void) session_name = opt_session_name; } - 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: + WARN("Tracing already stopped for session %s", session_name); + break; + default: + ERR("%s", lttng_strerror(ret)); + break; + } 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; + + print_session_stats(session_name); 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) { @@ -97,7 +164,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); @@ -106,23 +173,95 @@ int cmd_stop(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); goto end; default: - usage(stderr); ret = CMD_UNDEFINED; goto end; } } + /* 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; }