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 output_trace_archive_location(
61 const struct lttng_trace_archive_location
*location
,
62 const char *session_name
)
65 enum lttng_trace_archive_location_type location_type
;
66 enum lttng_trace_archive_location_status status
;
67 bool printed_location
= false;
69 location_type
= lttng_trace_archive_location_get_type(location
);
71 _MSG("Trace chunk archive for session %s is now readable",
73 switch (location_type
) {
74 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
:
76 const char *absolute_path
;
78 status
= lttng_trace_archive_location_local_get_absolute_path(
79 location
, &absolute_path
);
80 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
84 MSG(" at %s", absolute_path
);
85 printed_location
= true;
88 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
:
90 uint16_t control_port
, data_port
;
91 const char *host
, *relative_path
, *protocol_str
;
92 enum lttng_trace_archive_location_relay_protocol_type protocol
;
94 /* Fetch all relay location parameters. */
95 status
= lttng_trace_archive_location_relay_get_protocol_type(
97 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
102 status
= lttng_trace_archive_location_relay_get_host(
104 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
109 status
= lttng_trace_archive_location_relay_get_control_port(
110 location
, &control_port
);
111 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
116 status
= lttng_trace_archive_location_relay_get_data_port(
117 location
, &data_port
);
118 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
123 status
= lttng_trace_archive_location_relay_get_relative_path(
124 location
, &relative_path
);
125 if (status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
131 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
:
132 protocol_str
= "tcp";
135 protocol_str
= "unknown";
139 MSG(" on relay %s://%s/%s [control port %" PRIu16
", data port %"
140 PRIu16
"]", protocol_str
, host
,
141 relative_path
, control_port
, data_port
);
142 printed_location
= true;
149 if (!printed_location
) {
150 MSG(" at an unknown location");
155 static int rotate_tracing(char *session_name
)
158 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
159 struct lttng_rotation_handle
*handle
= NULL
;
160 enum lttng_rotation_status rotation_status
;
161 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
162 const struct lttng_trace_archive_location
*location
= NULL
;
163 bool print_location
= true;
165 DBG("Rotating the output files of session %s", session_name
);
167 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
170 case LTTNG_ERR_SESSION_NOT_STARTED
:
171 WARN("Tracing session %s not started yet", session_name
);
172 cmd_ret
= CMD_WARNING
;
175 ERR("%s", lttng_strerror(ret
));
181 rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
185 _MSG("Waiting for rotation to complete");
186 ret
= fflush(stdout
);
193 rotation_status
= lttng_rotation_handle_get_state(handle
,
195 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
196 ERR("Failed to query the state of the rotation.");
200 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
201 ret
= usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
208 ret
= fflush(stdout
);
214 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
218 switch (rotation_state
) {
219 case LTTNG_ROTATION_STATE_COMPLETED
:
220 rotation_status
= lttng_rotation_handle_get_archive_location(
222 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
223 ERR("Failed to retrieve the rotation's completed chunk archive location.");
227 case LTTNG_ROTATION_STATE_EXPIRED
:
229 case LTTNG_ROTATION_STATE_ERROR
:
230 ERR("Failed to retrieve rotation state.");
232 case LTTNG_ROTATION_STATE_ONGOING
:
233 MSG("Rotation ongoing for session %s", session_name
);
234 print_location
= false;
237 ERR("Unexpected rotation state encountered.");
241 if (!lttng_opt_mi
&& print_location
) {
242 ret
= output_trace_archive_location(location
,
244 } else if (lttng_opt_mi
) {
245 ret
= mi_lttng_rotate(writer
, session_name
, rotation_state
,
254 lttng_rotation_handle_destroy(handle
);
264 * The 'rotate <options>' first level command
266 int cmd_rotate(int argc
, const char **argv
)
269 enum cmd_error_code cmd_ret
= CMD_SUCCESS
;
271 static poptContext pc
;
272 char *session_name
= NULL
;
273 bool free_session_name
= false;
275 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
276 popt_ret
= poptReadDefaultConfig(pc
, 0);
278 ERR("poptReadDefaultConfig");
282 while ((opt
= poptGetNextOpt(pc
)) != -1) {
287 case OPT_LIST_OPTIONS
:
288 list_cmd_options(stdout
, long_options
);
291 cmd_ret
= CMD_UNDEFINED
;
296 opt_session_name
= (char*) poptGetArg(pc
);
298 if (!opt_session_name
) {
299 session_name
= get_session_name();
303 free_session_name
= true;
305 session_name
= opt_session_name
;
310 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
315 /* Open rotate command */
316 ret
= mi_lttng_writer_command_open(writer
,
317 mi_lttng_element_command_rotate
);
322 /* Open output element */
323 ret
= mi_lttng_writer_open_element(writer
,
324 mi_lttng_element_command_output
);
330 cmd_ret
= rotate_tracing(session_name
);
334 /* Close output element */
335 ret
= mi_lttng_writer_close_element(writer
);
340 ret
= mi_lttng_writer_write_element_bool(writer
,
341 mi_lttng_element_command_success
,
342 cmd_ret
== CMD_SUCCESS
);
347 /* Command element close */
348 ret
= mi_lttng_writer_command_close(writer
);
355 if (writer
&& mi_lttng_writer_destroy(writer
)) {
360 if (free_session_name
) {