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
;
147 const char *leftover
= NULL
;
149 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
150 poptReadDefaultConfig(pc
, 0);
152 while ((opt
= poptGetNextOpt(pc
)) != -1) {
157 case OPT_LIST_OPTIONS
:
158 list_cmd_options(stdout
, long_options
);
166 opt_session_name
= (char*) poptGetArg(pc
);
168 leftover
= poptGetArg(pc
);
170 ERR("Unknown argument: %s", leftover
);
177 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
179 ret
= -LTTNG_ERR_NOMEM
;
183 /* Open command element */
184 ret
= mi_lttng_writer_command_open(writer
,
185 mi_lttng_element_command_start
);
191 /* Open output element */
192 ret
= mi_lttng_writer_open_element(writer
,
193 mi_lttng_element_command_output
);
200 * Open sessions element
201 * For validation purpose
203 ret
= mi_lttng_writer_open_element(writer
,
204 config_element_sessions
);
211 command_ret
= start_tracing();
218 /* Close sessions and output element */
219 ret
= mi_lttng_close_multi_element(writer
, 2);
226 ret
= mi_lttng_writer_write_element_bool(writer
,
227 mi_lttng_element_command_success
, success
);
233 /* Command element close */
234 ret
= mi_lttng_writer_command_close(writer
);
243 if (writer
&& mi_lttng_writer_destroy(writer
)) {
244 /* Preserve original error code */
245 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
248 /* Overwrite ret if an error occurred with start_tracing */
249 ret
= command_ret
? command_ret
: ret
;