2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/mi-lttng.h>
30 #include "../command.h"
33 static char *opt_session_name
;
34 static struct mi_writer
*writer
;
36 #ifdef LTTNG_EMBED_HELP
37 static const char help_msg
[] =
38 #include <lttng-start.1.h>
47 static struct poptOption long_options
[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
50 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
54 static int mi_print_session(char *session_name
, int enabled
)
58 /* Open session element */
59 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
64 /* Print session name element */
65 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
71 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
77 /* Close session element */
78 ret
= mi_lttng_writer_close_element(writer
);
87 * Start tracing for all trace of the session.
89 static int start_tracing(void)
94 if (opt_session_name
== NULL
) {
95 session_name
= get_session_name();
96 if (session_name
== NULL
) {
101 session_name
= opt_session_name
;
104 DBG("Starting tracing for session %s", session_name
);
106 ret
= lttng_start_tracing(session_name
);
109 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
110 WARN("Tracing already started for session %s", session_name
);
113 ERR("%s", lttng_strerror(ret
));
121 MSG("Tracing started for session %s", session_name
);
123 ret
= mi_print_session(session_name
, 1);
131 if (opt_session_name
== NULL
) {
141 * The 'start <options>' first level command
143 int cmd_start(int argc
, const char **argv
)
145 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
146 static poptContext pc
;
148 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
149 poptReadDefaultConfig(pc
, 0);
151 while ((opt
= poptGetNextOpt(pc
)) != -1) {
156 case OPT_LIST_OPTIONS
:
157 list_cmd_options(stdout
, long_options
);
165 opt_session_name
= (char*) poptGetArg(pc
);
169 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
171 ret
= -LTTNG_ERR_NOMEM
;
175 /* Open command element */
176 ret
= mi_lttng_writer_command_open(writer
,
177 mi_lttng_element_command_start
);
183 /* Open output element */
184 ret
= mi_lttng_writer_open_element(writer
,
185 mi_lttng_element_command_output
);
192 * Open sessions element
193 * For validation purpose
195 ret
= mi_lttng_writer_open_element(writer
,
196 config_element_sessions
);
203 command_ret
= start_tracing();
210 /* Close sessions and output element */
211 ret
= mi_lttng_close_multi_element(writer
, 2);
218 ret
= mi_lttng_writer_write_element_bool(writer
,
219 mi_lttng_element_command_success
, success
);
225 /* Command element close */
226 ret
= mi_lttng_writer_command_close(writer
);
235 if (writer
&& mi_lttng_writer_destroy(writer
)) {
236 /* Preserve original error code */
237 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
240 /* Overwrite ret if an error occurred with start_tracing */
241 ret
= command_ret
? command_ret
: ret
;