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>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/mi-lttng.h>
31 #include "../command.h"
33 static char *opt_session_name
;
34 static int opt_no_wait
;
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
},
46 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
51 * Mi print of partial session
53 static int mi_print_session(char *session_name
, int enabled
)
59 /* Open session element */
60 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
65 /* Print session name element */
66 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
73 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
79 /* Close session element */
80 ret
= mi_lttng_writer_close_element(writer
);
87 * Start tracing for all trace of the session.
89 static int stop_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 ret
= lttng_stop_tracing_no_wait(session_name
);
107 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
108 WARN("Tracing already stopped for session %s", session_name
);
111 ERR("%s", lttng_strerror(ret
));
118 _MSG("Waiting for data availability");
121 ret
= lttng_data_pending(session_name
);
123 /* Return the data available call error. */
128 * Data sleep time before retrying (in usec). Don't sleep if the call
129 * returned value indicates availability.
132 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
142 print_session_stats(session_name
);
143 MSG("Tracing stopped for session %s", session_name
);
145 ret
= mi_print_session(session_name
, 0);
152 if (opt_session_name
== NULL
) {
163 * The 'stop <options>' first level command
165 int cmd_stop(int argc
, const char **argv
)
167 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
168 static poptContext pc
;
169 const char *leftover
= NULL
;
171 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
172 poptReadDefaultConfig(pc
, 0);
174 while ((opt
= poptGetNextOpt(pc
)) != -1) {
179 case OPT_LIST_OPTIONS
:
180 list_cmd_options(stdout
, long_options
);
190 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
192 ret
= -LTTNG_ERR_NOMEM
;
196 /* Open command element */
197 ret
= mi_lttng_writer_command_open(writer
,
198 mi_lttng_element_command_stop
);
204 /* Open output element */
205 ret
= mi_lttng_writer_open_element(writer
,
206 mi_lttng_element_command_output
);
213 * Open sessions element
216 ret
= mi_lttng_writer_open_element(writer
,
217 config_element_sessions
);
224 opt_session_name
= (char*) poptGetArg(pc
);
226 leftover
= poptGetArg(pc
);
228 ERR("Unknown argument: %s", leftover
);
233 command_ret
= stop_tracing();
240 /* Close sessions and output element */
241 ret
= mi_lttng_close_multi_element(writer
, 2);
248 ret
= mi_lttng_writer_write_element_bool(writer
,
249 mi_lttng_element_command_success
, success
);
255 /* Command element close */
256 ret
= mi_lttng_writer_command_close(writer
);
265 if (writer
&& mi_lttng_writer_destroy(writer
)) {
266 /* Preserve original error code */
267 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
270 /* Overwrite ret if an error occurred in stop_tracing() */
271 ret
= command_ret
? command_ret
: ret
;