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_already_stopped
;
73 enum lttng_error_code ret_code
;
74 struct lttng_destruction_handle
*handle
= NULL
;
75 enum lttng_destruction_handle_status status
;
76 bool newline_needed
= false, printed_destroy_msg
= false;
77 enum lttng_rotation_state rotation_state
;
78 char *stats_str
= NULL
;
80 ret
= lttng_stop_tracing_no_wait(session
->name
);
81 if (ret
< 0 && ret
!= -LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
82 ERR("%s", lttng_strerror(ret
));
85 session_was_already_stopped
= ret
== -LTTNG_ERR_TRACE_ALREADY_STOPPED
;
88 ret
= lttng_data_pending(session
->name
);
90 /* Return the data available call error. */
95 * Data sleep time before retrying (in usec). Don't
96 * sleep if the call returned value indicates
100 _MSG("Destroying session %s", session
->name
);
101 newline_needed
= true;
102 printed_destroy_msg
= true;
105 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
112 if (!session_was_already_stopped
) {
114 * Don't print the event and packet loss warnings since the user
115 * already saw them when stopping the trace.
117 ret
= get_session_stats_str(session
->name
, &stats_str
);
123 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
124 if (ret_code
!= LTTNG_OK
) {
130 goto skip_wait_rotation
;
134 status
= lttng_destruction_handle_wait_for_completion(
135 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/
138 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
139 if (!printed_destroy_msg
) {
140 _MSG("Destroying session %s", session
->name
);
141 newline_needed
= true;
142 printed_destroy_msg
= true;
147 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
150 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
151 newline_needed
? "\n" : "",
153 newline_needed
= false;
157 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
159 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
160 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
161 ERR("%sFailed to get the result of session destruction",
162 newline_needed
? "\n" : "");
164 newline_needed
= false;
167 if (ret_code
!= LTTNG_OK
) {
172 status
= lttng_destruction_handle_get_rotation_state(
173 handle
, &rotation_state
);
174 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
175 ERR("%sFailed to get rotation state from destruction handle",
176 newline_needed
? "\n" : "");
177 newline_needed
= false;
178 goto skip_wait_rotation
;
181 switch (rotation_state
) {
182 case LTTNG_ROTATION_STATE_NO_ROTATION
:
184 case LTTNG_ROTATION_STATE_COMPLETED
:
186 const struct lttng_trace_archive_location
*location
;
188 status
= lttng_destruction_handle_get_archive_location(
190 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
191 ret
= print_trace_archive_location(
192 location
, session
->name
);
194 ERR("%sFailed to print the location of trace archive",
195 newline_needed
? "\n" : "");
196 newline_needed
= false;
197 goto skip_wait_rotation
;
204 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
205 newline_needed
? "\n" : "");
206 newline_needed
= false;
207 goto skip_wait_rotation
;
210 MSG("%sSession %s destroyed", newline_needed
? "\n" : "",
212 newline_needed
= false;
214 MSG("%s", stats_str
);
217 session_name
= get_session_name_quiet();
218 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
219 config_destroy_default();
223 ret
= mi_lttng_session(writer
, session
, 0);
232 if (newline_needed
) {
235 lttng_destruction_handle_destroy(handle
);
242 * destroy_all_sessions
244 * Call destroy_sessions for each registered sessions
246 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
249 bool error_occurred
= false;
253 MSG("No session found, nothing to do.");
256 for (i
= 0; i
< count
; i
++) {
257 int ret
= destroy_session(&sessions
[i
]);
260 ERR("%s during the destruction of session \"%s\"",
263 /* Continue to next session. */
264 error_occurred
= true;
268 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
272 * The 'destroy <options>' first level command
274 int cmd_destroy(int argc
, const char **argv
)
277 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
278 static poptContext pc
;
279 char *session_name
= NULL
;
280 const char *leftover
= NULL
;
282 struct lttng_session
*sessions
;
286 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
287 poptReadDefaultConfig(pc
, 0);
289 while ((opt
= poptGetNextOpt(pc
)) != -1) {
294 case OPT_LIST_OPTIONS
:
295 list_cmd_options(stdout
, long_options
);
306 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
308 ret
= -LTTNG_ERR_NOMEM
;
312 /* Open command element */
313 ret
= mi_lttng_writer_command_open(writer
,
314 mi_lttng_element_command_destroy
);
320 /* Open output element */
321 ret
= mi_lttng_writer_open_element(writer
,
322 mi_lttng_element_command_output
);
328 /* For validation and semantic purpose we open a sessions element */
329 ret
= mi_lttng_sessions_open(writer
);
336 /* Recuperate all sessions for further operation */
337 count
= lttng_list_sessions(&sessions
);
339 ERR("%s", lttng_strerror(count
));
340 command_ret
= CMD_ERROR
;
345 /* Ignore session name in case all sessions are to be destroyed */
346 if (opt_destroy_all
) {
347 command_ret
= destroy_all_sessions(sessions
, count
);
352 opt_session_name
= (char *) poptGetArg(pc
);
354 if (!opt_session_name
) {
355 /* No session name specified, lookup default */
356 session_name
= get_session_name();
357 if (session_name
== NULL
) {
358 command_ret
= CMD_ERROR
;
363 session_name
= opt_session_name
;
366 /* Find the corresponding lttng_session struct */
368 for (i
= 0; i
< count
; i
++) {
369 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
371 command_ret
= destroy_session(&sessions
[i
]);
374 ERR("%s during the destruction of session \"%s\"",
375 lttng_strerror(command_ret
),
382 ERR("Session name %s not found", session_name
);
383 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
389 leftover
= poptGetArg(pc
);
391 ERR("Unknown argument: %s", leftover
);
400 /* Close sessions and output element element */
401 ret
= mi_lttng_close_multi_element(writer
, 2);
408 ret
= mi_lttng_writer_write_element_bool(writer
,
409 mi_lttng_element_command_success
, success
);
415 /* Command element close */
416 ret
= mi_lttng_writer_command_close(writer
);
424 if (writer
&& mi_lttng_writer_destroy(writer
)) {
425 /* Preserve original error code */
426 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
429 if (opt_session_name
== NULL
) {
433 /* Overwrite ret if an error occurred during destroy_session/all */
434 ret
= command_ret
? command_ret
: ret
;