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
;
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
51 static void usage(FILE *ofp
)
53 fprintf(ofp
, "usage: lttng start [NAME] [OPTIONS]\n");
55 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
56 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
58 fprintf(ofp
, "Options:\n");
59 fprintf(ofp
, " -h, --help Show this help\n");
60 fprintf(ofp
, " --list-options Simple listing of options\n");
64 static int mi_print_session(char *session_name
, int enabled
)
68 /* Open session element */
69 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
74 /* Print session name element */
75 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
81 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
87 /* Close session element */
88 ret
= mi_lttng_writer_close_element(writer
);
97 * Start tracing for all trace of the session.
99 static int start_tracing(void)
104 if (opt_session_name
== NULL
) {
105 session_name
= get_session_name();
106 if (session_name
== NULL
) {
111 session_name
= opt_session_name
;
114 DBG("Starting tracing for session %s", session_name
);
116 ret
= lttng_start_tracing(session_name
);
119 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
120 WARN("Tracing already started for session %s", session_name
);
123 ERR("%s", lttng_strerror(ret
));
131 MSG("Tracing started for session %s", session_name
);
133 ret
= mi_print_session(session_name
, 1);
141 if (opt_session_name
== NULL
) {
151 * The 'start <options>' first level command
153 int cmd_start(int argc
, const char **argv
)
155 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
156 static poptContext pc
;
158 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
159 poptReadDefaultConfig(pc
, 0);
161 while ((opt
= poptGetNextOpt(pc
)) != -1) {
166 case OPT_LIST_OPTIONS
:
167 list_cmd_options(stdout
, long_options
);
176 opt_session_name
= (char*) poptGetArg(pc
);
180 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
182 ret
= -LTTNG_ERR_NOMEM
;
186 /* Open command element */
187 ret
= mi_lttng_writer_command_open(writer
,
188 mi_lttng_element_command_start
);
194 /* Open output element */
195 ret
= mi_lttng_writer_open_element(writer
,
196 mi_lttng_element_command_output
);
203 * Open sessions element
204 * For validation purpose
206 ret
= mi_lttng_writer_open_element(writer
,
207 config_element_sessions
);
214 command_ret
= start_tracing();
221 /* Close sessions and output element */
222 ret
= mi_lttng_close_multi_element(writer
, 2);
229 ret
= mi_lttng_writer_write_element_bool(writer
,
230 mi_lttng_element_command_success
, success
);
236 /* Command element close */
237 ret
= mi_lttng_writer_command_close(writer
);
246 if (writer
&& mi_lttng_writer_destroy(writer
)) {
247 /* Preserve original error code */
248 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
251 /* Overwrite ret if an error occurred with start_tracing */
252 ret
= command_ret
? command_ret
: ret
;