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.
25 #include <sys/types.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/mi-lttng.h>
31 #include "../command.h"
34 static char *opt_session_name
;
35 static struct mi_writer
*writer
;
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 static void usage(FILE *ofp
)
54 fprintf(ofp
, "usage: lttng start [NAME] [OPTIONS]\n");
56 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
57 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
59 fprintf(ofp
, "Options:\n");
60 fprintf(ofp
, " -h, --help Show this help\n");
61 fprintf(ofp
, " --list-options Simple listing of options\n");
65 static int mi_print_session(char *session_name
, int enabled
)
69 /* Open session element */
70 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
75 /* Print session name element */
76 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
82 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
88 /* Close session element */
89 ret
= mi_lttng_writer_close_element(writer
);
98 * Start tracing for all trace of the session.
100 static int start_tracing(void)
105 if (opt_session_name
== NULL
) {
106 session_name
= get_session_name();
107 if (session_name
== NULL
) {
112 session_name
= opt_session_name
;
115 DBG("Starting tracing for session %s", session_name
);
117 ret
= lttng_start_tracing(session_name
);
120 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
121 WARN("Tracing already started for session %s", session_name
);
124 ERR("%s", lttng_strerror(ret
));
132 MSG("Tracing started for session %s", session_name
);
134 ret
= mi_print_session(session_name
, 1);
142 if (opt_session_name
== NULL
) {
152 * The 'start <options>' first level command
154 int cmd_start(int argc
, const char **argv
)
156 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
157 static poptContext pc
;
159 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
160 poptReadDefaultConfig(pc
, 0);
162 while ((opt
= poptGetNextOpt(pc
)) != -1) {
167 case OPT_LIST_OPTIONS
:
168 list_cmd_options(stdout
, long_options
);
177 opt_session_name
= (char*) poptGetArg(pc
);
181 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
183 ret
= -LTTNG_ERR_NOMEM
;
187 /* Open command element */
188 ret
= mi_lttng_writer_command_open(writer
,
189 mi_lttng_element_command_start
);
195 /* Open output element */
196 ret
= mi_lttng_writer_open_element(writer
,
197 mi_lttng_element_command_output
);
204 * Open sessions element
205 * For validation purpose
207 ret
= mi_lttng_writer_open_element(writer
,
208 config_element_sessions
);
215 command_ret
= start_tracing();
222 /* Close sessions and output element */
223 ret
= mi_lttng_close_multi_element(writer
, 2);
230 ret
= mi_lttng_writer_write_element_bool(writer
,
231 mi_lttng_element_command_success
, success
);
237 /* Command element close */
238 ret
= mi_lttng_writer_command_close(writer
);
247 if (writer
&& mi_lttng_writer_destroy(writer
)) {
248 /* Preserve original error code */
249 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
252 /* Overwrite ret if an error occurred with start_tracing */
253 ret
= command_ret
? command_ret
: ret
;