Print the location of trace chunk produced at session destruction
[lttng-tools.git] / src / bin / lttng / commands / destroy.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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 <stdbool.h>
27 #include <lttng/lttng.h>
28
29 #include "../command.h"
30
31 #include <common/mi-lttng.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/utils.h>
34
35 static char *opt_session_name;
36 static int opt_destroy_all;
37 static int opt_no_wait;
38
39 #ifdef LTTNG_EMBED_HELP
40 static const char help_msg[] =
41 #include <lttng-destroy.1.h>
42 ;
43 #endif
44
45 /* Mi writer */
46 static struct mi_writer *writer;
47
48 enum {
49 OPT_HELP = 1,
50 OPT_LIST_OPTIONS,
51 };
52
53 static struct poptOption long_options[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
56 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
57 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
58 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
59 {0, 0, 0, 0, 0, 0, 0}
60 };
61
62 /*
63 * destroy_session
64 *
65 * Unregister the provided session to the session daemon. On success, removes
66 * the default configuration.
67 */
68 static int destroy_session(struct lttng_session *session)
69 {
70 int ret;
71 char *session_name = NULL;
72 bool session_was_stopped;
73 enum lttng_error_code ret_code;
74 struct lttng_destruction_handle *handle = NULL;
75 enum lttng_destruction_handle_status status;
76 bool printed_wait_msg = false;
77 enum lttng_rotation_state rotation_state;
78
79 ret = lttng_stop_tracing_no_wait(session->name);
80 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
81 ERR("%s", lttng_strerror(ret));
82 }
83 session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
84 if (!opt_no_wait) {
85 do {
86 ret = lttng_data_pending(session->name);
87 if (ret < 0) {
88 /* Return the data available call error. */
89 goto error;
90 }
91
92 /*
93 * Data sleep time before retrying (in usec). Don't sleep if the call
94 * returned value indicates availability.
95 */
96 if (ret) {
97 if (!printed_wait_msg) {
98 _MSG("Waiting for destruction of session \"%s\"",
99 session->name);
100 printed_wait_msg = true;
101 fflush(stdout);
102 }
103
104 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
105 _MSG(".");
106 fflush(stdout);
107 }
108 } while (ret != 0);
109 }
110 if (!session_was_stopped) {
111 /*
112 * Don't print the event and packet loss warnings since the user
113 * already saw them when stopping the trace.
114 */
115 print_session_stats(session->name);
116 }
117
118 ret_code = lttng_destroy_session_ext(session->name, &handle);
119 if (ret_code != LTTNG_OK) {
120 ret = -ret_code;
121 goto error;
122 }
123
124 if (opt_no_wait) {
125 goto skip_wait_rotation;
126 }
127
128 do {
129 status = lttng_destruction_handle_wait_for_completion(handle,
130 DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
131 switch (status) {
132 case LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT:
133 if (!printed_wait_msg) {
134 _MSG("Waiting for destruction of session \"%s\"",
135 session->name);
136 printed_wait_msg = true;
137 }
138 _MSG(".");
139 fflush(stdout);
140 break;
141 case LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED:
142 break;
143 default:
144 ERR("Failed to wait for the completion of the destruction of session \"%s\"",
145 session->name);
146 ret = -1;
147 goto error;
148 }
149 } while (status == LTTNG_DESTRUCTION_HANDLE_STATUS_TIMEOUT);
150
151 status = lttng_destruction_handle_get_result(handle, &ret_code);
152 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
153 ERR("Failed to get the result of session destruction");
154 ret = -1;
155 goto error;
156 }
157 if (ret_code != LTTNG_OK) {
158 ret = -LTTNG_OK;
159 goto error;
160 }
161
162 status = lttng_destruction_handle_get_rotation_state(handle,
163 &rotation_state);
164 switch (rotation_state) {
165 case LTTNG_ROTATION_STATE_NO_ROTATION:
166 break;
167 case LTTNG_ROTATION_STATE_COMPLETED:
168 {
169 const struct lttng_trace_archive_location *location;
170
171 status = lttng_destruction_handle_get_archive_location(handle,
172 &location);
173 if (status == LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
174 if (printed_wait_msg) {
175 MSG("");
176 printed_wait_msg = false;
177 }
178 ret = print_trace_archive_location(location,
179 session->name);
180 if (ret) {
181 ERR("Failed to print the location of trace archive");
182 goto skip_wait_rotation;
183 }
184 break;
185 }
186 /* fall-through. */
187 }
188 default:
189 ERR("Failed to get the location of the rotation performed during the session's destruction");
190 goto skip_wait_rotation;
191 }
192 skip_wait_rotation:
193 MSG("%sSession \"%s\" destroyed", printed_wait_msg ? "\n" : "",
194 session->name);
195
196 session_name = get_session_name_quiet();
197 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
198 config_destroy_default();
199 }
200
201 if (lttng_opt_mi) {
202 ret = mi_lttng_session(writer, session, 0);
203 if (ret) {
204 ret = CMD_ERROR;
205 goto error;
206 }
207 }
208
209 ret = CMD_SUCCESS;
210 error:
211 lttng_destruction_handle_destroy(handle);
212 free(session_name);
213 return ret;
214 }
215
216 /*
217 * destroy_all_sessions
218 *
219 * Call destroy_sessions for each registered sessions
220 */
221 static int destroy_all_sessions(struct lttng_session *sessions, int count)
222 {
223 int i, ret = CMD_SUCCESS;
224
225 if (count == 0) {
226 MSG("No session found, nothing to do.");
227 } else if (count < 0) {
228 ERR("%s", lttng_strerror(ret));
229 goto error;
230 }
231
232 for (i = 0; i < count; i++) {
233 ret = destroy_session(&sessions[i]);
234 if (ret < 0) {
235 goto error;
236 }
237 }
238 error:
239 return ret;
240 }
241
242 /*
243 * The 'destroy <options>' first level command
244 */
245 int cmd_destroy(int argc, const char **argv)
246 {
247 int opt;
248 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
249 static poptContext pc;
250 char *session_name = NULL;
251 const char *leftover = NULL;
252
253 struct lttng_session *sessions;
254 int count;
255 int found;
256
257 pc = poptGetContext(NULL, argc, argv, long_options, 0);
258 poptReadDefaultConfig(pc, 0);
259
260 while ((opt = poptGetNextOpt(pc)) != -1) {
261 switch (opt) {
262 case OPT_HELP:
263 SHOW_HELP();
264 break;
265 case OPT_LIST_OPTIONS:
266 list_cmd_options(stdout, long_options);
267 break;
268 default:
269 ret = CMD_UNDEFINED;
270 break;
271 }
272 goto end;
273 }
274
275 /* Mi preparation */
276 if (lttng_opt_mi) {
277 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
278 if (!writer) {
279 ret = -LTTNG_ERR_NOMEM;
280 goto end;
281 }
282
283 /* Open command element */
284 ret = mi_lttng_writer_command_open(writer,
285 mi_lttng_element_command_destroy);
286 if (ret) {
287 ret = CMD_ERROR;
288 goto end;
289 }
290
291 /* Open output element */
292 ret = mi_lttng_writer_open_element(writer,
293 mi_lttng_element_command_output);
294 if (ret) {
295 ret = CMD_ERROR;
296 goto end;
297 }
298
299 /* For validation and semantic purpose we open a sessions element */
300 ret = mi_lttng_sessions_open(writer);
301 if (ret) {
302 ret = CMD_ERROR;
303 goto end;
304 }
305 }
306
307 /* Recuperate all sessions for further operation */
308 count = lttng_list_sessions(&sessions);
309 if (count < 0) {
310 ERR("%s", lttng_strerror(count));
311 command_ret = CMD_ERROR;
312 success = 0;
313 goto mi_closing;
314 }
315
316 /* Ignore session name in case all sessions are to be destroyed */
317 if (opt_destroy_all) {
318 command_ret = destroy_all_sessions(sessions, count);
319 if (command_ret) {
320 success = 0;
321 }
322 } else {
323 opt_session_name = (char *) poptGetArg(pc);
324
325 if (!opt_session_name) {
326 /* No session name specified, lookup default */
327 session_name = get_session_name();
328 if (session_name == NULL) {
329 command_ret = CMD_ERROR;
330 success = 0;
331 goto mi_closing;
332 }
333 } else {
334 session_name = opt_session_name;
335 }
336
337 /* Find the corresponding lttng_session struct */
338 found = 0;
339 for (i = 0; i < count; i++) {
340 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
341 found = 1;
342 command_ret = destroy_session(&sessions[i]);
343 if (command_ret) {
344 success = 0;
345 }
346
347 }
348 }
349
350 if (!found) {
351 ERR("Session name %s not found", session_name);
352 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
353 success = 0;
354 goto mi_closing;
355 }
356 }
357
358 leftover = poptGetArg(pc);
359 if (leftover) {
360 ERR("Unknown argument: %s", leftover);
361 ret = CMD_ERROR;
362 success = 0;
363 goto mi_closing;
364 }
365
366 mi_closing:
367 /* Mi closing */
368 if (lttng_opt_mi) {
369 /* Close sessions and output element element */
370 ret = mi_lttng_close_multi_element(writer, 2);
371 if (ret) {
372 ret = CMD_ERROR;
373 goto end;
374 }
375
376 /* Success ? */
377 ret = mi_lttng_writer_write_element_bool(writer,
378 mi_lttng_element_command_success, success);
379 if (ret) {
380 ret = CMD_ERROR;
381 goto end;
382 }
383
384 /* Command element close */
385 ret = mi_lttng_writer_command_close(writer);
386 if (ret) {
387 ret = CMD_ERROR;
388 goto end;
389 }
390 }
391 end:
392 /* Mi clean-up */
393 if (writer && mi_lttng_writer_destroy(writer)) {
394 /* Preserve original error code */
395 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
396 }
397
398 if (opt_session_name == NULL) {
399 free(session_name);
400 }
401
402 /* Overwrite ret if an error occurred during destroy_session/all */
403 ret = command_ret ? command_ret : ret;
404
405 poptFreeContext(pc);
406 return ret;
407 }
This page took 0.037956 seconds and 5 git commands to generate.