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>
27 #include <lttng/lttng.h>
29 #include "../command.h"
31 #include <common/mi-lttng.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/utils.h>
35 static char *opt_session_name
;
36 static int opt_destroy_all
;
37 static int opt_no_wait
;
39 #ifdef LTTNG_EMBED_HELP
40 static const char help_msg
[] =
41 #include <lttng-destroy.1.h>
46 static struct mi_writer
*writer
;
53 static struct poptOption long_options
[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
56 {"all", 'a', POPT_ARG_VAL
, &opt_destroy_all
, 1, 0, 0},
57 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
58 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
65 * Unregister the provided session to the session daemon. On success, removes
66 * the default configuration.
68 static int destroy_session(struct lttng_session
*session
)
71 char *session_name
= NULL
;
72 bool session_was_stopped
;
73 enum lttng_error_code ret_code
;
74 struct lttng_destruction_handle
*handle
= NULL
;
75 enum lttng_destruction_handle_status status
;
76 bool printed_wait_msg
= false;
77 enum lttng_rotation_state rotation_state
;
79 ret
= lttng_stop_tracing_no_wait(session
->name
);
80 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
81 ERR("%s", lttng_strerror(ret
));
83 session_was_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
86 ret
= lttng_data_pending(session
->name
);
88 /* Return the data available call error. */
93 * Data sleep time before retrying (in usec). Don't sleep if the call
94 * returned value indicates availability.
97 if (!printed_wait_msg
) {
98 _MSG("Waiting for destruction of session \"%s\"",
100 printed_wait_msg
= true;
104 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
110 if (!session_was_stopped
) {
112 * Don't print the event and packet loss warnings since the user
113 * already saw them when stopping the trace.
115 print_session_stats(session
->name
);
118 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
119 if (ret_code
!= LTTNG_OK
) {
125 goto skip_wait_rotation
;
129 status
= lttng_destruction_handle_wait_for_completion(handle
,
130 DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/ USEC_PER_MSEC
);
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
133 if (!printed_wait_msg
) {
134 _MSG("Waiting for destruction of session \"%s\"",
136 printed_wait_msg
= true;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
144 ERR("Failed to wait for the completion of the destruction of session \"%s\"",
149 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
151 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
152 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
153 ERR("Failed to get the result of session destruction");
157 if (ret_code
!= LTTNG_OK
) {
162 status
= lttng_destruction_handle_get_rotation_state(handle
,
164 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
165 ERR("Failed to get rotation state from destruction handle");
166 goto skip_wait_rotation
;
168 switch (rotation_state
) {
169 case LTTNG_ROTATION_STATE_NO_ROTATION
:
171 case LTTNG_ROTATION_STATE_COMPLETED
:
173 const struct lttng_trace_archive_location
*location
;
175 status
= lttng_destruction_handle_get_archive_location(handle
,
177 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
178 if (printed_wait_msg
) {
180 printed_wait_msg
= false;
182 ret
= print_trace_archive_location(location
,
185 ERR("Failed to print the location of trace archive");
186 goto skip_wait_rotation
;
193 ERR("Failed to get the location of the rotation performed during the session's destruction");
194 goto skip_wait_rotation
;
197 MSG("%sSession \"%s\" destroyed", printed_wait_msg
? "\n" : "",
199 printed_wait_msg
= false;
201 session_name
= get_session_name_quiet();
202 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
203 config_destroy_default();
207 ret
= mi_lttng_session(writer
, session
, 0);
216 if (printed_wait_msg
) {
219 lttng_destruction_handle_destroy(handle
);
225 * destroy_all_sessions
227 * Call destroy_sessions for each registered sessions
229 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
232 bool error_occurred
= false;
236 MSG("No session found, nothing to do.");
239 for (i
= 0; i
< count
; i
++) {
240 int ret
= destroy_session(&sessions
[i
]);
243 ERR("%s during the destruction of session \"%s\"",
246 /* Continue to next session. */
247 error_occurred
= true;
251 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
255 * The 'destroy <options>' first level command
257 int cmd_destroy(int argc
, const char **argv
)
260 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
261 static poptContext pc
;
262 char *session_name
= NULL
;
263 const char *leftover
= NULL
;
265 struct lttng_session
*sessions
;
269 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
270 poptReadDefaultConfig(pc
, 0);
272 while ((opt
= poptGetNextOpt(pc
)) != -1) {
277 case OPT_LIST_OPTIONS
:
278 list_cmd_options(stdout
, long_options
);
289 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
291 ret
= -LTTNG_ERR_NOMEM
;
295 /* Open command element */
296 ret
= mi_lttng_writer_command_open(writer
,
297 mi_lttng_element_command_destroy
);
303 /* Open output element */
304 ret
= mi_lttng_writer_open_element(writer
,
305 mi_lttng_element_command_output
);
311 /* For validation and semantic purpose we open a sessions element */
312 ret
= mi_lttng_sessions_open(writer
);
319 /* Recuperate all sessions for further operation */
320 count
= lttng_list_sessions(&sessions
);
322 ERR("%s", lttng_strerror(count
));
323 command_ret
= CMD_ERROR
;
328 /* Ignore session name in case all sessions are to be destroyed */
329 if (opt_destroy_all
) {
330 command_ret
= destroy_all_sessions(sessions
, count
);
335 opt_session_name
= (char *) poptGetArg(pc
);
337 if (!opt_session_name
) {
338 /* No session name specified, lookup default */
339 session_name
= get_session_name();
340 if (session_name
== NULL
) {
341 command_ret
= CMD_ERROR
;
346 session_name
= opt_session_name
;
349 /* Find the corresponding lttng_session struct */
351 for (i
= 0; i
< count
; i
++) {
352 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
354 command_ret
= destroy_session(&sessions
[i
]);
357 ERR("%s during the destruction of session \"%s\"",
358 lttng_strerror(command_ret
),
365 ERR("Session name %s not found", session_name
);
366 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
372 leftover
= poptGetArg(pc
);
374 ERR("Unknown argument: %s", leftover
);
383 /* Close sessions and output element element */
384 ret
= mi_lttng_close_multi_element(writer
, 2);
391 ret
= mi_lttng_writer_write_element_bool(writer
,
392 mi_lttng_element_command_success
, success
);
398 /* Command element close */
399 ret
= mi_lttng_writer_command_close(writer
);
407 if (writer
&& mi_lttng_writer_destroy(writer
)) {
408 /* Preserve original error code */
409 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
412 if (opt_session_name
== NULL
) {
416 /* Overwrite ret if an error occurred during destroy_session/all */
417 ret
= command_ret
? command_ret
: ret
;