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 if (!printed_destroy_msg
) {
101 _MSG("Destroying session %s",
103 newline_needed
= true;
104 printed_destroy_msg
= true;
108 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
115 if (!session_was_already_stopped
) {
117 * Don't print the event and packet loss warnings since the user
118 * already saw them when stopping the trace.
120 ret
= get_session_stats_str(session
->name
, &stats_str
);
126 ret_code
= lttng_destroy_session_ext(session
->name
, &handle
);
127 if (ret_code
!= LTTNG_OK
) {
133 goto skip_wait_rotation
;
137 status
= lttng_destruction_handle_wait_for_completion(
138 handle
, DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
/
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
:
142 if (!printed_destroy_msg
) {
143 _MSG("Destroying session %s", session
->name
);
144 newline_needed
= true;
145 printed_destroy_msg
= true;
150 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED
:
153 ERR("%sFailed to wait for the completion of the destruction of session \"%s\"",
154 newline_needed
? "\n" : "",
156 newline_needed
= false;
160 } while (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT
);
162 status
= lttng_destruction_handle_get_result(handle
, &ret_code
);
163 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
164 ERR("%sFailed to get the result of session destruction",
165 newline_needed
? "\n" : "");
167 newline_needed
= false;
170 if (ret_code
!= LTTNG_OK
) {
175 status
= lttng_destruction_handle_get_rotation_state(
176 handle
, &rotation_state
);
177 if (status
!= LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
178 ERR("%sFailed to get rotation state from destruction handle",
179 newline_needed
? "\n" : "");
180 newline_needed
= false;
181 goto skip_wait_rotation
;
184 switch (rotation_state
) {
185 case LTTNG_ROTATION_STATE_NO_ROTATION
:
187 case LTTNG_ROTATION_STATE_COMPLETED
:
189 const struct lttng_trace_archive_location
*location
;
191 status
= lttng_destruction_handle_get_archive_location(
193 if (status
== LTTNG_DESTRUCTION_HANDLE_STATUS_OK
) {
194 ret
= print_trace_archive_location(
195 location
, session
->name
);
197 ERR("%sFailed to print the location of trace archive",
198 newline_needed
? "\n" : "");
199 newline_needed
= false;
200 goto skip_wait_rotation
;
207 ERR("%sFailed to get the location of the rotation performed during the session's destruction",
208 newline_needed
? "\n" : "");
209 newline_needed
= false;
210 goto skip_wait_rotation
;
213 MSG("%sSession %s destroyed", newline_needed
? "\n" : "",
215 newline_needed
= false;
217 MSG("%s", stats_str
);
220 session_name
= get_session_name_quiet();
221 if (session_name
&& !strncmp(session
->name
, session_name
, NAME_MAX
)) {
222 config_destroy_default();
226 ret
= mi_lttng_session(writer
, session
, 0);
235 if (newline_needed
) {
238 lttng_destruction_handle_destroy(handle
);
245 * destroy_all_sessions
247 * Call destroy_sessions for each registered sessions
249 static int destroy_all_sessions(struct lttng_session
*sessions
, int count
)
252 bool error_occurred
= false;
256 MSG("No session found, nothing to do.");
259 for (i
= 0; i
< count
; i
++) {
260 int ret
= destroy_session(&sessions
[i
]);
263 ERR("%s during the destruction of session \"%s\"",
266 /* Continue to next session. */
267 error_occurred
= true;
271 return error_occurred
? CMD_ERROR
: CMD_SUCCESS
;
275 * The 'destroy <options>' first level command
277 int cmd_destroy(int argc
, const char **argv
)
280 int ret
= CMD_SUCCESS
, i
, command_ret
= CMD_SUCCESS
, success
= 1;
281 static poptContext pc
;
282 char *session_name
= NULL
;
283 const char *leftover
= NULL
;
285 struct lttng_session
*sessions
;
289 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
290 poptReadDefaultConfig(pc
, 0);
292 while ((opt
= poptGetNextOpt(pc
)) != -1) {
297 case OPT_LIST_OPTIONS
:
298 list_cmd_options(stdout
, long_options
);
309 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
311 ret
= -LTTNG_ERR_NOMEM
;
315 /* Open command element */
316 ret
= mi_lttng_writer_command_open(writer
,
317 mi_lttng_element_command_destroy
);
323 /* Open output element */
324 ret
= mi_lttng_writer_open_element(writer
,
325 mi_lttng_element_command_output
);
331 /* For validation and semantic purpose we open a sessions element */
332 ret
= mi_lttng_sessions_open(writer
);
339 /* Recuperate all sessions for further operation */
340 count
= lttng_list_sessions(&sessions
);
342 ERR("%s", lttng_strerror(count
));
343 command_ret
= CMD_ERROR
;
348 /* Ignore session name in case all sessions are to be destroyed */
349 if (opt_destroy_all
) {
350 command_ret
= destroy_all_sessions(sessions
, count
);
355 opt_session_name
= (char *) poptGetArg(pc
);
357 if (!opt_session_name
) {
358 /* No session name specified, lookup default */
359 session_name
= get_session_name();
360 if (session_name
== NULL
) {
361 command_ret
= CMD_ERROR
;
366 session_name
= opt_session_name
;
369 /* Find the corresponding lttng_session struct */
371 for (i
= 0; i
< count
; i
++) {
372 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
374 command_ret
= destroy_session(&sessions
[i
]);
377 ERR("%s during the destruction of session \"%s\"",
378 lttng_strerror(command_ret
),
385 ERR("Session name %s not found", session_name
);
386 command_ret
= LTTNG_ERR_SESS_NOT_FOUND
;
392 leftover
= poptGetArg(pc
);
394 ERR("Unknown argument: %s", leftover
);
403 /* Close sessions and output element element */
404 ret
= mi_lttng_close_multi_element(writer
, 2);
411 ret
= mi_lttng_writer_write_element_bool(writer
,
412 mi_lttng_element_command_success
, success
);
418 /* Command element close */
419 ret
= mi_lttng_writer_command_close(writer
);
427 if (writer
&& mi_lttng_writer_destroy(writer
)) {
428 /* Preserve original error code */
429 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
432 if (opt_session_name
== NULL
) {
436 /* Overwrite ret if an error occurred during destroy_session/all */
437 ret
= command_ret
? command_ret
: ret
;