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>
36 static char *opt_session_name
;
37 static int opt_no_wait
;
38 static struct mi_writer
*writer
;
45 static struct poptOption long_options
[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
49 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
53 static int mi_output_rotate(const char *status
, const char *path
,
54 const char *session_name
)
63 ret
= mi_lttng_writer_open_element(writer
,
64 mi_lttng_element_rotation
);
69 ret
= mi_lttng_writer_write_element_string(writer
,
70 mi_lttng_element_session_name
, session_name
);
75 ret
= mi_lttng_writer_write_element_string(writer
,
76 mi_lttng_element_rotate_status
, status
);
81 ret
= mi_lttng_writer_write_element_string(writer
,
82 config_element_path
, path
);
87 /* Close rotation element */
88 ret
= mi_lttng_writer_close_element(writer
);
97 static int rotate_tracing(char *session_name
)
100 struct lttng_rotation_immediate_attr
*attr
= NULL
;
101 struct lttng_rotation_handle
*handle
= NULL
;
102 enum lttng_rotation_status rotation_status
;
103 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
105 DBG("Rotating the output files of session %s", session_name
);
107 attr
= lttng_rotation_immediate_attr_create();
112 ret
= lttng_rotation_immediate_attr_set_session_name(attr
, session_name
);
114 ERR("Session name exceeds the maximal allowed length");
118 ret
= lttng_rotate_session(attr
, &handle
);
121 case LTTNG_ERR_SESSION_NOT_STARTED
:
122 WARN("Tracing session %s not started yet", session_name
);
125 ERR("%s", lttng_strerror(ret
));
132 _MSG("Waiting for rotation to complete");
133 ret
= fflush(stdout
);
140 rotation_status
= lttng_rotation_handle_get_state(handle
,
142 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
143 ERR("Failed to query the state of the rotation");
148 * Data sleep time before retrying (in usec). Don't
149 * sleep if the call returned value indicates
152 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
153 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
160 ret
= fflush(stdout
);
166 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
170 switch (rotation_state
) {
171 case LTTNG_ROTATION_STATE_COMPLETED
:
175 rotation_status
= lttng_rotation_handle_get_completed_archive_location(
177 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
178 ERR("Failed to retrieve the rotation's completed chunk archive location");
181 MSG("Trace chunk archive for session %s is now readable at %s",
183 ret
= mi_output_rotate("completed", path
, session_name
);
190 case LTTNG_ROTATION_STATE_EXPIRED
:
191 MSG("Session %s rotated, but handle expired", session_name
);
192 ret
= mi_output_rotate("expired", NULL
, session_name
);
199 ERR("Unexpected rotation state received, aborting...");
206 lttng_rotation_handle_destroy(handle
);
207 lttng_rotation_immediate_attr_destroy(attr
);
214 * The 'rotate <options>' first level command
216 int cmd_rotate(int argc
, const char **argv
)
218 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
220 static poptContext pc
;
221 char *session_name
= NULL
;
222 bool free_session_name
= false;
224 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
225 popt_ret
= poptReadDefaultConfig(pc
, 0);
228 ERR("poptReadDefaultConfig");
232 while ((opt
= poptGetNextOpt(pc
)) != -1) {
237 case OPT_LIST_OPTIONS
:
238 list_cmd_options(stdout
, long_options
);
246 opt_session_name
= (char*) poptGetArg(pc
);
248 if (!opt_session_name
) {
249 session_name
= get_session_name();
253 free_session_name
= true;
255 session_name
= opt_session_name
;
260 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
262 ret
= -LTTNG_ERR_NOMEM
;
266 /* Open rotate command */
267 ret
= mi_lttng_writer_command_open(writer
,
268 mi_lttng_element_command_rotate
);
274 /* Open output element */
275 ret
= mi_lttng_writer_open_element(writer
,
276 mi_lttng_element_command_output
);
281 /* Open rotations element */
282 ret
= mi_lttng_writer_open_element(writer
,
283 mi_lttng_element_rotations
);
290 command_ret
= rotate_tracing(session_name
);
297 /* Close rotations element */
298 ret
= mi_lttng_writer_close_element(writer
);
302 /* Close output element */
303 ret
= mi_lttng_writer_close_element(writer
);
308 ret
= mi_lttng_writer_write_element_bool(writer
,
309 mi_lttng_element_command_success
, success
);
315 /* Command element close */
316 ret
= mi_lttng_writer_command_close(writer
);
325 if (writer
&& mi_lttng_writer_destroy(writer
)) {
326 /* Preserve original error code */
327 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
330 /* Overwrite ret if an error occurred with start_tracing */
331 ret
= command_ret
? command_ret
: ret
;
333 if (free_session_name
) {