Display discarded and lost events at destroy and stop
[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
28 #include "../command.h"
29
30 #include <common/mi-lttng.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/utils.h>
33
34 static char *opt_session_name;
35 static int opt_destroy_all;
36 static int opt_no_wait;
37
38 /* Mi writer */
39 static struct mi_writer *writer;
40
41 enum {
42 OPT_HELP = 1,
43 OPT_LIST_OPTIONS,
44 };
45
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 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
50 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
51 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
52 {0, 0, 0, 0, 0, 0, 0}
53 };
54
55 /*
56 * usage
57 */
58 static void usage(FILE *ofp)
59 {
60 fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n");
61 fprintf(ofp, "\n");
62 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
63 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
64 fprintf(ofp, "\n");
65 fprintf(ofp, "Options:\n");
66 fprintf(ofp, " -h, --help Show this help\n");
67 fprintf(ofp, " -a, --all Destroy all sessions\n");
68 fprintf(ofp, " --list-options Simple listing of options\n");
69 fprintf(ofp, " -n, --no-wait Don't wait for data availability\n");
70 fprintf(ofp, "\n");
71 }
72
73 /*
74 * destroy_session
75 *
76 * Unregister the provided session to the session daemon. On success, removes
77 * the default configuration.
78 */
79 static int destroy_session(struct lttng_session *session)
80 {
81 int ret;
82 char *session_name = NULL;
83 bool session_was_stopped;
84
85 ret = lttng_stop_tracing_no_wait(session->name);
86 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
87 ERR("%s", lttng_strerror(ret));
88 }
89 session_was_stopped = ret == -LTTNG_ERR_TRACE_ALREADY_STOPPED;
90 if (!opt_no_wait) {
91 _MSG("Waiting for data availability");
92 fflush(stdout);
93 do {
94 ret = lttng_data_pending(session->name);
95 if (ret < 0) {
96 /* Return the data available call error. */
97 goto error;
98 }
99
100 /*
101 * Data sleep time before retrying (in usec). Don't sleep if the call
102 * returned value indicates availability.
103 */
104 if (ret) {
105 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
106 _MSG(".");
107 fflush(stdout);
108 }
109 } while (ret != 0);
110 MSG("");
111 }
112 if (!session_was_stopped) {
113 /*
114 * Don't print the event and packet loss warnings since the user
115 * already saw them when stopping the trace.
116 */
117 print_session_stats(session->name);
118 }
119
120 ret = lttng_destroy_session_no_wait(session->name);
121 if (ret < 0) {
122 goto error;
123 }
124
125 MSG("Session %s destroyed", session->name);
126
127 session_name = get_session_name_quiet();
128 if (session_name && !strncmp(session->name, session_name, NAME_MAX)) {
129 config_destroy_default();
130 }
131
132 if (lttng_opt_mi) {
133 ret = mi_lttng_session(writer, session, 0);
134 if (ret) {
135 ret = CMD_ERROR;
136 goto error;
137 }
138 }
139
140 ret = CMD_SUCCESS;
141 error:
142 free(session_name);
143 return ret;
144 }
145
146 /*
147 * destroy_all_sessions
148 *
149 * Call destroy_sessions for each registered sessions
150 */
151 static int destroy_all_sessions(struct lttng_session *sessions, int count)
152 {
153 int i, ret = CMD_SUCCESS;
154
155 if (count == 0) {
156 MSG("No session found, nothing to do.");
157 } else if (count < 0) {
158 ERR("%s", lttng_strerror(ret));
159 goto error;
160 }
161
162 for (i = 0; i < count; i++) {
163 ret = destroy_session(&sessions[i]);
164 if (ret < 0) {
165 goto error;
166 }
167 }
168 error:
169 return ret;
170 }
171
172 /*
173 * The 'destroy <options>' first level command
174 */
175 int cmd_destroy(int argc, const char **argv)
176 {
177 int opt;
178 int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
179 static poptContext pc;
180 char *session_name = NULL;
181
182 struct lttng_session *sessions;
183 int count;
184 int found;
185
186 pc = poptGetContext(NULL, argc, argv, long_options, 0);
187 poptReadDefaultConfig(pc, 0);
188
189 while ((opt = poptGetNextOpt(pc)) != -1) {
190 switch (opt) {
191 case OPT_HELP:
192 usage(stdout);
193 break;
194 case OPT_LIST_OPTIONS:
195 list_cmd_options(stdout, long_options);
196 break;
197 default:
198 usage(stderr);
199 ret = CMD_UNDEFINED;
200 break;
201 }
202 goto end;
203 }
204
205 /* Mi preparation */
206 if (lttng_opt_mi) {
207 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
208 if (!writer) {
209 ret = -LTTNG_ERR_NOMEM;
210 goto end;
211 }
212
213 /* Open command element */
214 ret = mi_lttng_writer_command_open(writer,
215 mi_lttng_element_command_destroy);
216 if (ret) {
217 ret = CMD_ERROR;
218 goto end;
219 }
220
221 /* Open output element */
222 ret = mi_lttng_writer_open_element(writer,
223 mi_lttng_element_command_output);
224 if (ret) {
225 ret = CMD_ERROR;
226 goto end;
227 }
228
229 /* For validation and semantic purpose we open a sessions element */
230 ret = mi_lttng_sessions_open(writer);
231 if (ret) {
232 ret = CMD_ERROR;
233 goto end;
234 }
235 }
236
237 /* Recuperate all sessions for further operation */
238 count = lttng_list_sessions(&sessions);
239 if (count < 0) {
240 command_ret = count;
241 success = 0;
242 goto mi_closing;
243 }
244
245 /* Ignore session name in case all sessions are to be destroyed */
246 if (opt_destroy_all) {
247 command_ret = destroy_all_sessions(sessions, count);
248 if (command_ret) {
249 success = 0;
250 }
251 } else {
252 opt_session_name = (char *) poptGetArg(pc);
253
254 if (!opt_session_name) {
255 /* No session name specified, lookup default */
256 session_name = get_session_name();
257 if (session_name == NULL) {
258 command_ret = CMD_ERROR;
259 success = 0;
260 goto mi_closing;
261 }
262 } else {
263 session_name = opt_session_name;
264 }
265
266 /* Find the corresponding lttng_session struct */
267 found = 0;
268 for (i = 0; i < count; i++) {
269 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
270 found = 1;
271 command_ret = destroy_session(&sessions[i]);
272 if (command_ret) {
273 success = 0;
274 }
275
276 }
277 }
278
279 if (!found) {
280 ERR("Session name %s not found", session_name);
281 command_ret = LTTNG_ERR_SESS_NOT_FOUND;
282 success = 0;
283 goto mi_closing;
284 }
285 }
286
287 mi_closing:
288 /* Mi closing */
289 if (lttng_opt_mi) {
290 /* Close sessions and output element element */
291 ret = mi_lttng_close_multi_element(writer, 2);
292 if (ret) {
293 ret = CMD_ERROR;
294 goto end;
295 }
296
297 /* Success ? */
298 ret = mi_lttng_writer_write_element_bool(writer,
299 mi_lttng_element_command_success, success);
300 if (ret) {
301 ret = CMD_ERROR;
302 goto end;
303 }
304
305 /* Command element close */
306 ret = mi_lttng_writer_command_close(writer);
307 if (ret) {
308 ret = CMD_ERROR;
309 goto end;
310 }
311 }
312 end:
313 /* Mi clean-up */
314 if (writer && mi_lttng_writer_destroy(writer)) {
315 /* Preserve original error code */
316 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
317 }
318
319 if (opt_session_name == NULL) {
320 free(session_name);
321 }
322
323 /* Overwrite ret if an error occurred during destroy_session/all */
324 ret = command_ret ? command_ret : ret;
325
326 poptFreeContext(pc);
327 return ret;
328 }
This page took 0.037148 seconds and 5 git commands to generate.