Port: Remove _GNU_SOURCE, defined in config.h
[lttng-tools.git] / src / bin / lttng / commands / start.c
index 1db0a31c5b49a48efea44cdbd841d0801849c9f4..b353eb6e3888174483ac52932145f575a1089f92 100644 (file)
@@ -1,22 +1,21 @@
 /*
  * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
  *
- * 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 <popt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <common/sessiond-comm/sessiond-comm.h>
+#include <common/mi-lttng.h>
+
 #include "../command.h"
 
+
 static char *opt_session_name;
+static struct mi_writer *writer;
 
 enum {
        OPT_HELP = 1,
@@ -46,16 +50,47 @@ static struct poptOption long_options[] = {
  */
 static void usage(FILE *ofp)
 {
-       fprintf(ofp, "usage: lttng start [options] [NAME]\n");
+       fprintf(ofp, "usage: lttng start [NAME] [OPTIONS]\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, "Options:\n");
        fprintf(ofp, "  -h, --help               Show this help\n");
        fprintf(ofp, "      --list-options       Simple listing of options\n");
        fprintf(ofp, "\n");
 }
 
+static int mi_print_session(char *session_name, int enabled)
+{
+       int ret;
+
+       /* 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;
+       }
+
+       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
  *
@@ -63,7 +98,7 @@ static void usage(FILE *ofp)
  */
 static int start_tracing(void)
 {
-       int ret = CMD_SUCCESS;
+       int ret;
        char *session_name;
 
        if (opt_session_name == NULL) {
@@ -80,10 +115,27 @@ static int start_tracing(void)
 
        ret = lttng_start_tracing(session_name);
        if (ret < 0) {
+               switch (-ret) {
+               case LTTNG_ERR_TRACE_ALREADY_STARTED:
+                       WARN("Tracing already started for session %s", session_name);
+                       break;
+               default:
+                       ERR("%s", lttng_strerror(ret));
+                       break;
+               }
                goto free_name;
        }
 
+       ret = CMD_SUCCESS;
+
        MSG("Tracing started for session %s", session_name);
+       if (lttng_opt_mi) {
+               ret = mi_print_session(session_name, 1);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto free_name;
+               }
+       }
 
 free_name:
        if (opt_session_name == NULL) {
@@ -100,7 +152,7 @@ error:
  */
 int cmd_start(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);
@@ -123,9 +175,81 @@ int cmd_start(int argc, const char **argv)
 
        opt_session_name = (char*) poptGetArg(pc);
 
-       ret = start_tracing();
+       /* 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_start);
+               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 purpose
+                */
+               ret = mi_lttng_writer_open_element(writer,
+                       config_element_sessions);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
+       command_ret = start_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 with start_tracing */
+       ret = command_ret ? command_ret : ret;
        poptFreeContext(pc);
        return ret;
 }
This page took 0.025303 seconds and 4 git commands to generate.