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>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/mi-lttng.h>
32 #include "../command.h"
33 #include <lttng/rotate.h>
35 static char *opt_session_name
;
36 static int opt_no_wait
;
37 static struct mi_writer
*writer
;
46 static struct poptOption long_options
[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
50 {"no-wait", 'n', POPT_ARG_VAL
, &opt_no_wait
, 1, 0, 0},
51 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
52 {"timer", 0, POPT_ARG_INT
, 0, OPT_TIMER
, 0, 0},
53 {"size", 0, POPT_ARG_INT
, 0, OPT_SIZE
, 0, 0},
57 static int mi_print_session(char *session_name
, int enabled
)
61 /* Open session element */
62 ret
= mi_lttng_writer_open_element(writer
, config_element_session
);
67 /* Print session name element */
68 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
74 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
80 /* Close session element */
81 ret
= mi_lttng_writer_close_element(writer
);
87 static int setup_rotate(char *session_name
, uint64_t timer
, uint64_t size
)
90 struct lttng_rotate_session_attr
*attr
= NULL
;
92 attr
= lttng_rotate_session_attr_create();
97 ret
= lttng_rotate_session_attr_set_session_name(attr
, session_name
);
102 lttng_rotate_session_attr_set_timer(attr
, timer
);
103 lttng_rotate_session_attr_set_size(attr
, size
);
106 DBG("Configuring session %s to rotate every %" PRIu64
" us",
107 session_name
, timer
);
110 DBG("Configuring session %s to rotate every %" PRIu64
" bytes written",
114 ret
= lttng_rotate_setup(attr
);
124 static int rotate_tracing(char *session_name
)
128 struct lttng_rotate_session_attr
*attr
= NULL
;
129 struct lttng_rotate_session_handle
*handle
= NULL
;
130 enum lttng_rotate_status rotate_status
;
132 attr
= lttng_rotate_session_attr_create();
137 ret
= lttng_rotate_session_attr_set_session_name(attr
, session_name
);
142 DBG("Rotating the output files of session %s", session_name
);
144 ret
= lttng_rotate_session(attr
, &handle
);
147 case LTTNG_ERR_SESSION_NOT_STARTED
:
148 WARN("Tracing session %s not started yet", session_name
);
151 ERR("%s", lttng_strerror(ret
));
158 _MSG("Waiting for data availability");
161 ret
= lttng_rotate_session_pending(handle
);
167 * Data sleep time before retrying (in usec). Don't sleep if the call
168 * returned value indicates availability.
171 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
179 rotate_status
= lttng_rotate_session_get_status(handle
);
180 switch(rotate_status
) {
181 case LTTNG_ROTATE_COMPLETED
:
182 lttng_rotate_session_get_output_path(handle
, &path
);
183 MSG("Output files of session %s rotated to %s", session_name
, path
);
186 case LTTNG_ROTATE_STARTED
:
187 MSG("Rotation started for session %s", session_name
);
190 ret
= mi_print_session(session_name
, 1);
199 case LTTNG_ROTATE_EXPIRED
:
200 MSG("Output files of session %s rotated, but handle expired", session_name
);
202 ret
= mi_print_session(session_name
, 1);
211 case LTTNG_ROTATE_ERROR
:
212 MSG("An error occurred with the rotation of session %s", session_name
);
214 ret
= mi_print_session(session_name
, 1);
223 case LTTNG_ROTATE_EMPTY
:
224 MSG("Empty session, nothing to rotate.");
232 lttng_rotate_session_handle_destroy(handle
);
233 lttng_rotate_session_attr_destroy(attr
);
240 * The 'rotate <options>' first level command
242 int cmd_rotate(int argc
, const char **argv
)
244 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
245 static poptContext pc
;
246 char *session_name
= NULL
;
247 char *opt_arg
= NULL
;
248 uint64_t timer
= 0, size
= 0;
250 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
251 poptReadDefaultConfig(pc
, 0);
253 while ((opt
= poptGetNextOpt(pc
)) != -1) {
258 case OPT_LIST_OPTIONS
:
259 list_cmd_options(stdout
, long_options
);
264 opt_arg
= poptGetOptArg(pc
);
265 timer
= strtoull(opt_arg
, NULL
, 0);
266 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
267 ERR("Wrong value in --timer parameter: %s", opt_arg
);
271 DBG("Rotation timer set to %" PRIu64
, timer
);
277 opt_arg
= poptGetOptArg(pc
);
278 size
= strtoull(opt_arg
, NULL
, 0);
279 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
280 ERR("Wrong value in --timer parameter: %s", opt_arg
);
284 DBG("Rotation size set to %" PRIu64
, size
);
293 if (opt_session_name
== NULL
) {
294 session_name
= get_session_name();
295 if (session_name
== NULL
) {
299 session_name
= opt_session_name
;
304 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
306 ret
= -LTTNG_ERR_NOMEM
;
310 /* Open command element */
311 ret
= mi_lttng_writer_command_open(writer
,
312 mi_lttng_element_command_start
);
318 /* Open output element */
319 ret
= mi_lttng_writer_open_element(writer
,
320 mi_lttng_element_command_output
);
327 * Open sessions element
328 * For validation purpose
330 ret
= mi_lttng_writer_open_element(writer
,
331 config_element_sessions
);
338 /* No config options, just rotate the session now */
339 if (timer
== 0 && size
== 0) {
340 command_ret
= rotate_tracing(session_name
);
342 command_ret
= setup_rotate(session_name
, timer
, size
);
345 ERR("%s", lttng_strerror(command_ret
));
351 /* Close sessions and output element */
352 ret
= mi_lttng_close_multi_element(writer
, 2);
359 ret
= mi_lttng_writer_write_element_bool(writer
,
360 mi_lttng_element_command_success
, success
);
366 /* Command element close */
367 ret
= mi_lttng_writer_command_close(writer
);
376 if (writer
&& mi_lttng_writer_destroy(writer
)) {
377 /* Preserve original error code */
378 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
381 /* Overwrite ret if an error occurred with start_tracing */
382 ret
= command_ret
? command_ret
: ret
;