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},
53 static void usage(FILE *ofp
)
55 fprintf(ofp
, "usage: lttng stop [NAME] [OPTIONS]\n");
57 fprintf(ofp
, "Where NAME is an optional session name. If not specified, lttng will\n");
58 fprintf(ofp
, "get it from the configuration directory (.lttng).\n");
60 fprintf(ofp
, "Options:\n");
61 fprintf(ofp
, " -h, --help Show this help\n");
62 fprintf(ofp
, " --list-options Simple listing of options\n");
63 fprintf(ofp
, " -n, --no-wait Don't wait for data availability\n");
67 * Mi print of partial session
69 static int mi_print_session(char *session_name
, int enabled
)
75 /* Open session element */
76 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
81 /* Print session name element */
82 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
89 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
95 /* Close session element */
96 ret
= mi_lttng_writer_close_element(writer
);
103 * Start tracing for all trace of the session.
105 static int stop_tracing(void)
110 if (opt_session_name
== NULL
) {
111 session_name
= get_session_name();
112 if (session_name
== NULL
) {
117 session_name
= opt_session_name
;
120 ret
= lttng_stop_tracing_no_wait(session_name
);
123 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
124 WARN("Tracing already stopped for session %s", session_name
);
127 ERR("%s", lttng_strerror(ret
));
134 _MSG("Waiting for data availability");
137 ret
= lttng_data_pending(session_name
);
139 /* Return the data available call error. */
144 * Data sleep time before retrying (in usec). Don't sleep if the call
145 * returned value indicates availability.
148 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
158 print_session_stats(session_name
);
159 MSG("Tracing stopped for session %s", session_name
);
161 ret
= mi_print_session(session_name
, 0);
168 if (opt_session_name
== NULL
) {
179 * The 'stop <options>' first level command
181 int cmd_stop(int argc
, const char **argv
)
183 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
184 static poptContext pc
;
186 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
187 poptReadDefaultConfig(pc
, 0);
189 while ((opt
= poptGetNextOpt(pc
)) != -1) {
194 case OPT_LIST_OPTIONS
:
195 list_cmd_options(stdout
, long_options
);
206 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
208 ret
= -LTTNG_ERR_NOMEM
;
212 /* Open command element */
213 ret
= mi_lttng_writer_command_open(writer
,
214 mi_lttng_element_command_stop
);
220 /* Open output element */
221 ret
= mi_lttng_writer_open_element(writer
,
222 mi_lttng_element_command_output
);
229 * Open sessions element
232 ret
= mi_lttng_writer_open_element(writer
,
233 config_element_sessions
);
240 opt_session_name
= (char*) poptGetArg(pc
);
242 command_ret
= stop_tracing();
249 /* Close sessions and output element */
250 ret
= mi_lttng_close_multi_element(writer
, 2);
257 ret
= mi_lttng_writer_write_element_bool(writer
,
258 mi_lttng_element_command_success
, success
);
264 /* Command element close */
265 ret
= mi_lttng_writer_command_close(writer
);
274 if (writer
&& mi_lttng_writer_destroy(writer
)) {
275 /* Preserve original error code */
276 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
279 /* Overwrite ret if an error occurred in stop_tracing() */
280 ret
= command_ret
? command_ret
: ret
;