2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/sessiond-comm/sessiond-comm.h>
19 #include <common/mi-lttng.h>
21 #include "../command.h"
23 static char *opt_session_name
;
24 static int opt_no_wait
;
25 static struct mi_writer
*writer
;
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-stop.1.h>
38 static struct poptOption long_options
[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
41 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
42 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
47 * Mi print of partial session
49 static int mi_print_session(char *session_name
, int enabled
)
55 /* Open session element */
56 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
61 /* Print session name element */
62 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
69 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
75 /* Close session element */
76 ret
= mi_lttng_writer_close_element(writer
);
83 * Start tracing for all trace of the session.
85 static int stop_tracing(void)
90 if (opt_session_name
== NULL
) {
91 session_name
= get_session_name();
92 if (session_name
== NULL
) {
97 session_name
= opt_session_name
;
100 ret
= lttng_stop_tracing_no_wait(session_name
);
103 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
104 WARN("Tracing already stopped for session %s", session_name
);
107 ERR("%s", lttng_strerror(ret
));
114 _MSG("Waiting for data availability");
117 ret
= lttng_data_pending(session_name
);
119 /* Return the data available call error. */
124 * Data sleep time before retrying (in usec). Don't sleep if the call
125 * returned value indicates availability.
128 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
138 print_session_stats(session_name
);
139 MSG("Tracing stopped for session %s", session_name
);
141 ret
= mi_print_session(session_name
, 0);
148 if (opt_session_name
== NULL
) {
159 * The 'stop <options>' first level command
161 int cmd_stop(int argc
, const char **argv
)
163 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
164 static poptContext pc
;
165 const char *leftover
= NULL
;
167 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
168 poptReadDefaultConfig(pc
, 0);
170 while ((opt
= poptGetNextOpt(pc
)) != -1) {
175 case OPT_LIST_OPTIONS
:
176 list_cmd_options(stdout
, long_options
);
186 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
188 ret
= -LTTNG_ERR_NOMEM
;
192 /* Open command element */
193 ret
= mi_lttng_writer_command_open(writer
,
194 mi_lttng_element_command_stop
);
200 /* Open output element */
201 ret
= mi_lttng_writer_open_element(writer
,
202 mi_lttng_element_command_output
);
209 * Open sessions element
212 ret
= mi_lttng_writer_open_element(writer
,
213 config_element_sessions
);
220 opt_session_name
= (char*) poptGetArg(pc
);
222 leftover
= poptGetArg(pc
);
224 ERR("Unknown argument: %s", leftover
);
229 command_ret
= stop_tracing();
236 /* Close sessions and output element */
237 ret
= mi_lttng_close_multi_element(writer
, 2);
244 ret
= mi_lttng_writer_write_element_bool(writer
,
245 mi_lttng_element_command_success
, success
);
251 /* Command element close */
252 ret
= mi_lttng_writer_command_close(writer
);
261 if (writer
&& mi_lttng_writer_destroy(writer
)) {
262 /* Preserve original error code */
263 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
266 /* Overwrite ret if an error occurred in stop_tracing() */
267 ret
= command_ret
? command_ret
: ret
;