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