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
;
37 #ifdef LTTNG_EMBED_HELP
38 static const char help_msg
[] =
39 #include <lttng-stop.1.h>
48 static struct poptOption long_options
[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
51 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
52 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
57 * Mi print of partial session
59 static int mi_print_session(char *session_name
, int enabled
)
65 /* Open session element */
66 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
71 /* Print session name element */
72 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
79 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
85 /* Close session element */
86 ret
= mi_lttng_writer_close_element(writer
);
93 * Start tracing for all trace of the session.
95 static int stop_tracing(void)
100 if (opt_session_name
== NULL
) {
101 session_name
= get_session_name();
102 if (session_name
== NULL
) {
107 session_name
= opt_session_name
;
110 ret
= lttng_stop_tracing_no_wait(session_name
);
113 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
114 WARN("Tracing already stopped for session %s", session_name
);
117 ERR("%s", lttng_strerror(ret
));
124 _MSG("Waiting for data availability");
127 ret
= lttng_data_pending(session_name
);
129 /* Return the data available call error. */
134 * Data sleep time before retrying (in usec). Don't sleep if the call
135 * returned value indicates availability.
138 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
148 print_session_stats(session_name
);
149 MSG("Tracing stopped for session %s", session_name
);
151 ret
= mi_print_session(session_name
, 0);
158 if (opt_session_name
== NULL
) {
169 * The 'stop <options>' first level command
171 int cmd_stop(int argc
, const char **argv
)
173 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
174 static poptContext pc
;
175 const char *leftover
= NULL
;
177 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
178 poptReadDefaultConfig(pc
, 0);
180 while ((opt
= poptGetNextOpt(pc
)) != -1) {
185 case OPT_LIST_OPTIONS
:
186 list_cmd_options(stdout
, long_options
);
196 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
198 ret
= -LTTNG_ERR_NOMEM
;
202 /* Open command element */
203 ret
= mi_lttng_writer_command_open(writer
,
204 mi_lttng_element_command_stop
);
210 /* Open output element */
211 ret
= mi_lttng_writer_open_element(writer
,
212 mi_lttng_element_command_output
);
219 * Open sessions element
222 ret
= mi_lttng_writer_open_element(writer
,
223 config_element_sessions
);
230 opt_session_name
= (char*) poptGetArg(pc
);
232 leftover
= poptGetArg(pc
);
234 ERR("Unknown argument: %s", leftover
);
239 command_ret
= stop_tracing();
246 /* Close sessions and output element */
247 ret
= mi_lttng_close_multi_element(writer
, 2);
254 ret
= mi_lttng_writer_write_element_bool(writer
,
255 mi_lttng_element_command_success
, success
);
261 /* Command element close */
262 ret
= mi_lttng_writer_command_close(writer
);
271 if (writer
&& mi_lttng_writer_destroy(writer
)) {
272 /* Preserve original error code */
273 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
276 /* Overwrite ret if an error occurred in stop_tracing() */
277 ret
= command_ret
? command_ret
: ret
;