2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/sessiond-comm/sessiond-comm.hpp>
18 #include <common/mi-lttng.hpp>
20 #include "../command.hpp"
22 static char *opt_session_name
;
23 static int opt_no_wait
;
24 static struct mi_writer
*writer
;
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-stop.1.h>
37 static struct poptOption long_options
[] = {
38 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
39 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
40 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
41 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
46 * Mi print of partial session
48 static int mi_print_session(char *session_name
, int enabled
)
52 LTTNG_ASSERT(session_name
);
54 /* Open session element */
55 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
60 /* Print session name element */
61 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
68 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
74 /* Close session element */
75 ret
= mi_lttng_writer_close_element(writer
);
82 * Start tracing for all trace of the session.
84 static int stop_tracing(void)
89 if (opt_session_name
== NULL
) {
90 session_name
= get_session_name();
91 if (session_name
== NULL
) {
96 session_name
= opt_session_name
;
99 ret
= lttng_stop_tracing_no_wait(session_name
);
102 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
103 WARN("Tracing already stopped for session %s", session_name
);
106 ERR("%s", lttng_strerror(ret
));
113 _MSG("Waiting for data availability");
116 ret
= lttng_data_pending(session_name
);
118 /* Return the data available call error. */
123 * Data sleep time before retrying (in usec). Don't sleep if the call
124 * returned value indicates availability.
127 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
137 print_session_stats(session_name
);
138 MSG("Tracing stopped for session %s", session_name
);
140 ret
= mi_print_session(session_name
, 0);
147 if (opt_session_name
== NULL
) {
158 * The 'stop <options>' first level command
160 int cmd_stop(int argc
, const char **argv
)
162 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
163 static poptContext pc
;
164 const char *leftover
= NULL
;
166 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
167 poptReadDefaultConfig(pc
, 0);
169 while ((opt
= poptGetNextOpt(pc
)) != -1) {
174 case OPT_LIST_OPTIONS
:
175 list_cmd_options(stdout
, long_options
);
185 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
187 ret
= -LTTNG_ERR_NOMEM
;
191 /* Open command element */
192 ret
= mi_lttng_writer_command_open(writer
,
193 mi_lttng_element_command_stop
);
199 /* Open output element */
200 ret
= mi_lttng_writer_open_element(writer
,
201 mi_lttng_element_command_output
);
208 * Open sessions element
211 ret
= mi_lttng_writer_open_element(writer
,
212 config_element_sessions
);
219 opt_session_name
= (char*) poptGetArg(pc
);
221 leftover
= poptGetArg(pc
);
223 ERR("Unknown argument: %s", leftover
);
228 command_ret
= stop_tracing();
235 /* Close sessions and output element */
236 ret
= mi_lttng_close_multi_element(writer
, 2);
243 ret
= mi_lttng_writer_write_element_bool(writer
,
244 mi_lttng_element_command_success
, success
);
250 /* Command element close */
251 ret
= mi_lttng_writer_command_close(writer
);
260 if (writer
&& mi_lttng_writer_destroy(writer
)) {
261 /* Preserve original error code */
262 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
265 /* Overwrite ret if an error occurred in stop_tracing() */
266 ret
= command_ret
? command_ret
: ret
;