2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
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>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/mi-lttng.h>
33 #include "../command.h"
34 #include <lttng/rotation.h>
35 #include <lttng/location.h>
37 static char *opt_session_name
;
38 static int opt_no_wait
;
39 static struct mi_writer
*writer
;
41 #ifdef LTTNG_EMBED_HELP
42 static const char help_msg
[] =
43 #include <lttng-rotate.1.h>
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
56 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
60 static int rotate_tracing(char *session_name
)
63 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
64 struct lttng_rotation_handle
*handle
= NULL
;
65 enum lttng_rotation_status rotation_status
;
66 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
67 const struct lttng_trace_archive_location
*location
= NULL
;
68 bool print_location
= true;
70 DBG("Rotating the output files of session %s", session_name
);
72 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
75 case LTTNG_ERR_SESSION_NOT_STARTED
:
76 WARN("Tracing session %s not started yet", session_name
);
77 cmd_ret
= CMD_WARNING
;
80 ERR("%s", lttng_strerror(ret
));
86 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
90 _MSG("Waiting for rotation to complete");
98 rotation_status
= lttng_rotation_handle_get_state(handle
,
100 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
102 ERR("Failed to query the state of the rotation.");
106 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
107 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US
);
114 ret
= fflush(stdout
);
120 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
124 switch (rotation_state
) {
125 case LTTNG_ROTATION_STATE_COMPLETED
:
126 rotation_status
= lttng_rotation_handle_get_archive_location(
128 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
129 ERR("Failed to retrieve the rotation's completed chunk archive location.");
133 case LTTNG_ROTATION_STATE_EXPIRED
:
135 case LTTNG_ROTATION_STATE_ERROR
:
136 ERR("Failed to retrieve rotation state.");
138 case LTTNG_ROTATION_STATE_ONGOING
:
139 MSG("Rotation ongoing for session %s", session_name
);
140 print_location
= false;
143 ERR("Unexpected rotation state encountered.");
147 if (!lttng_opt_mi
&& print_location
) {
148 ret
= print_trace_archive_location(location
,
150 } else if (lttng_opt_mi
) {
151 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
160 lttng_rotation_handle_destroy(handle
);
170 * The 'rotate <options>' first level command
172 int cmd_rotate(int argc
, const char **argv
)
175 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
177 static poptContext pc
;
178 char *session_name
= NULL
;
179 bool free_session_name
= false;
181 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
182 popt_ret
= poptReadDefaultConfig(pc
, 0);
184 ERR("poptReadDefaultConfig");
188 while ((opt
= poptGetNextOpt(pc
)) != -1) {
193 case OPT_LIST_OPTIONS
:
194 list_cmd_options(stdout
, long_options
);
197 cmd_ret
= CMD_UNDEFINED
;
202 opt_session_name
= (char*) poptGetArg(pc
);
204 if (!opt_session_name
) {
205 session_name
= get_session_name();
209 free_session_name
= true;
211 session_name
= opt_session_name
;
216 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
221 /* Open rotate command */
222 ret
= mi_lttng_writer_command_open(writer
,
223 mi_lttng_element_command_rotate
);
228 /* Open output element */
229 ret
= mi_lttng_writer_open_element(writer
,
230 mi_lttng_element_command_output
);
236 cmd_ret
= rotate_tracing(session_name
);
240 /* Close output element */
241 ret
= mi_lttng_writer_close_element(writer
);
246 ret
= mi_lttng_writer_write_element_bool(writer
,
247 mi_lttng_element_command_success
,
248 cmd_ret
== CMD_SUCCESS
);
253 /* Command element close */
254 ret
= mi_lttng_writer_command_close(writer
);
261 if (writer
&& mi_lttng_writer_destroy(writer
)) {
266 if (free_session_name
) {