Error out if filter expression is attached to unsupported event types
[lttng-tools.git] / src / bin / lttng / commands / rotate.c
CommitLineData
d68c9a04
JD
1/*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18#define _LGPL_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26#include <inttypes.h>
27#include <ctype.h>
28#include <assert.h>
29
30#include <common/sessiond-comm/sessiond-comm.h>
31#include <common/mi-lttng.h>
32
33#include "../command.h"
34#include <lttng/rotation.h>
dd73d57b 35#include <lttng/location.h>
d68c9a04
JD
36
37static char *opt_session_name;
38static int opt_no_wait;
39static struct mi_writer *writer;
40
3ae5c539
JD
41#ifdef LTTNG_EMBED_HELP
42static const char help_msg[] =
43#include <lttng-rotate.1.h>
44;
45#endif
46
d68c9a04
JD
47enum {
48 OPT_HELP = 1,
49 OPT_LIST_OPTIONS,
50};
51
52static 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},
57 {0, 0, 0, 0, 0, 0, 0}
58};
59
dd73d57b
JG
60static int output_trace_archive_location(
61 const struct lttng_trace_archive_location *location,
62 const char *session_name)
63{
64 int ret = 0;
65 enum lttng_trace_archive_location_type location_type;
66 enum lttng_trace_archive_location_status status;
67 bool printed_location = false;
68
69 location_type = lttng_trace_archive_location_get_type(location);
70
71 _MSG("Trace chunk archive for session %s is now readable",
72 session_name);
73 switch (location_type) {
74 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
75 {
76 const char *absolute_path;
77
78 status = lttng_trace_archive_location_local_get_absolute_path(
79 location, &absolute_path);
80 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
81 ret = -1;
82 goto end;
83 }
84 MSG(" at %s", absolute_path);
dd73d57b
JG
85 printed_location = true;
86 break;
87 }
88 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
89 {
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;
93
94 /* Fetch all relay location parameters. */
95 status = lttng_trace_archive_location_relay_get_protocol_type(
96 location, &protocol);
97 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
98 ret = -1;
99 goto end;
100 }
101
102 status = lttng_trace_archive_location_relay_get_host(
103 location, &host);
104 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
105 ret = -1;
106 goto end;
107 }
108
109 status = lttng_trace_archive_location_relay_get_control_port(
110 location, &control_port);
111 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
112 ret = -1;
113 goto end;
114 }
115
116 status = lttng_trace_archive_location_relay_get_data_port(
117 location, &data_port);
118 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
119 ret = -1;
120 goto end;
121 }
122
123 status = lttng_trace_archive_location_relay_get_relative_path(
124 location, &relative_path);
125 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
126 ret = -1;
127 goto end;
128 }
129
130 switch (protocol) {
131 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
132 protocol_str = "tcp";
133 break;
134 default:
135 protocol_str = "unknown";
136 break;
137 }
138
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;
dd73d57b
JG
143 break;
144 }
145 default:
146 break;
147 }
148end:
149 if (!printed_location) {
150 MSG(" at an unknown location");
151 }
152 return ret;
153}
154
d68c9a04
JD
155static int rotate_tracing(char *session_name)
156{
157 int ret;
91c4d516 158 enum cmd_error_code cmd_ret = CMD_SUCCESS;
d68c9a04
JD
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;
91c4d516
JG
162 const struct lttng_trace_archive_location *location = NULL;
163 bool print_location = true;
d68c9a04
JD
164
165 DBG("Rotating the output files of session %s", session_name);
166
dbd512ea 167 ret = lttng_rotate_session(session_name, NULL, &handle);
d68c9a04
JD
168 if (ret < 0) {
169 switch (-ret) {
170 case LTTNG_ERR_SESSION_NOT_STARTED:
171 WARN("Tracing session %s not started yet", session_name);
91c4d516
JG
172 cmd_ret = CMD_WARNING;
173 goto end;
d68c9a04
JD
174 default:
175 ERR("%s", lttng_strerror(ret));
91c4d516 176 goto error;
d68c9a04 177 }
91c4d516
JG
178 }
179
180 if (opt_no_wait) {
181 rotation_state = LTTNG_ROTATION_STATE_ONGOING;
182 goto skip_wait;
183 }
184
185 _MSG("Waiting for rotation to complete");
186 ret = fflush(stdout);
187 if (ret) {
188 PERROR("fflush");
d68c9a04
JD
189 goto error;
190 }
191
91c4d516
JG
192 do {
193 rotation_status = lttng_rotation_handle_get_state(handle,
194 &rotation_state);
195 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
196 ERR("Failed to query the state of the rotation.");
d68c9a04
JD
197 goto error;
198 }
199
91c4d516
JG
200 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
201 ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
202 if (ret) {
203 PERROR("usleep");
d68c9a04
JD
204 goto error;
205 }
91c4d516 206 _MSG(".");
d68c9a04 207
91c4d516
JG
208 ret = fflush(stdout);
209 if (ret) {
210 PERROR("fflush");
211 goto error;
d68c9a04 212 }
91c4d516
JG
213 }
214 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
215 MSG("");
d68c9a04 216
91c4d516 217skip_wait:
d68c9a04
JD
218 switch (rotation_state) {
219 case LTTNG_ROTATION_STATE_COMPLETED:
dd73d57b
JG
220 rotation_status = lttng_rotation_handle_get_archive_location(
221 handle, &location);
d68c9a04 222 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
91c4d516
JG
223 ERR("Failed to retrieve the rotation's completed chunk archive location.");
224 cmd_ret = CMD_ERROR;
d68c9a04 225 }
91c4d516 226 break;
d68c9a04 227 case LTTNG_ROTATION_STATE_EXPIRED:
91c4d516
JG
228 break;
229 case LTTNG_ROTATION_STATE_ERROR:
230 ERR("Failed to retrieve rotation state.");
231 print_location = false;
232 goto error;
233 case LTTNG_ROTATION_STATE_ONGOING:
234 MSG("Rotation ongoing for session %s", session_name);
235 print_location = false;
236 break;
d68c9a04 237 default:
91c4d516
JG
238 ERR("Unexpected rotation state encountered.");
239 print_location = false;
d68c9a04
JD
240 goto error;
241 }
242
91c4d516
JG
243 if (!lttng_opt_mi && print_location) {
244 ret = output_trace_archive_location(location,
245 session_name);
246 } else if (lttng_opt_mi) {
247 ret = mi_lttng_rotate(writer, session_name, rotation_state,
248 location);
249 }
250
251 if (ret < 0) {
252 cmd_ret = CMD_ERROR;
253 }
254
d68c9a04
JD
255end:
256 lttng_rotation_handle_destroy(handle);
91c4d516
JG
257 return cmd_ret;
258error:
259 cmd_ret = CMD_ERROR;
260 goto end;
d68c9a04
JD
261}
262
263/*
264 * cmd_rotate
265 *
266 * The 'rotate <options>' first level command
267 */
268int cmd_rotate(int argc, const char **argv)
269{
91c4d516
JG
270 int opt, ret;
271 enum cmd_error_code cmd_ret = CMD_SUCCESS;
d68c9a04
JD
272 int popt_ret;
273 static poptContext pc;
274 char *session_name = NULL;
275 bool free_session_name = false;
276
277 pc = poptGetContext(NULL, argc, argv, long_options, 0);
278 popt_ret = poptReadDefaultConfig(pc, 0);
279 if (popt_ret) {
d68c9a04 280 ERR("poptReadDefaultConfig");
91c4d516 281 goto error;
d68c9a04
JD
282 }
283
284 while ((opt = poptGetNextOpt(pc)) != -1) {
285 switch (opt) {
286 case OPT_HELP:
287 SHOW_HELP();
288 goto end;
289 case OPT_LIST_OPTIONS:
290 list_cmd_options(stdout, long_options);
291 goto end;
292 default:
91c4d516 293 cmd_ret = CMD_UNDEFINED;
d68c9a04
JD
294 goto end;
295 }
296 }
297
298 opt_session_name = (char*) poptGetArg(pc);
299
300 if (!opt_session_name) {
301 session_name = get_session_name();
302 if (!session_name) {
91c4d516 303 goto error;
d68c9a04
JD
304 }
305 free_session_name = true;
306 } else {
307 session_name = opt_session_name;
308 }
309
310 /* Mi check */
311 if (lttng_opt_mi) {
312 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
313 if (!writer) {
91c4d516 314 goto error;
d68c9a04
JD
315 }
316
317 /* Open rotate command */
318 ret = mi_lttng_writer_command_open(writer,
319 mi_lttng_element_command_rotate);
320 if (ret) {
91c4d516 321 goto error;
d68c9a04
JD
322 }
323
324 /* Open output element */
325 ret = mi_lttng_writer_open_element(writer,
326 mi_lttng_element_command_output);
327 if (ret) {
91c4d516 328 goto error;
d68c9a04 329 }
d68c9a04
JD
330 }
331
91c4d516 332 cmd_ret = rotate_tracing(session_name);
d68c9a04
JD
333
334 /* Mi closing */
335 if (lttng_opt_mi) {
66ea93b1 336 /* Close output element */
d68c9a04
JD
337 ret = mi_lttng_writer_close_element(writer);
338 if (ret) {
91c4d516 339 goto error;
d68c9a04
JD
340 }
341 /* Success ? */
342 ret = mi_lttng_writer_write_element_bool(writer,
91c4d516
JG
343 mi_lttng_element_command_success,
344 cmd_ret == CMD_SUCCESS);
d68c9a04 345 if (ret) {
91c4d516 346 goto error;
d68c9a04
JD
347 }
348
349 /* Command element close */
350 ret = mi_lttng_writer_command_close(writer);
351 if (ret) {
91c4d516 352 goto error;
d68c9a04
JD
353 }
354 }
355
d68c9a04
JD
356 /* Mi clean-up */
357 if (writer && mi_lttng_writer_destroy(writer)) {
91c4d516 358 goto error;
d68c9a04 359 }
91c4d516 360end:
d68c9a04
JD
361 poptFreeContext(pc);
362 if (free_session_name) {
363 free(session_name);
364 }
91c4d516
JG
365
366 return cmd_ret;
367error:
368 cmd_ret = CMD_ERROR;
369 goto end;
d68c9a04 370}
This page took 0.040969 seconds and 4 git commands to generate.