relayd: register fd tracker instance to all created trace chunks
[lttng-tools.git] / src / bin / lttng / commands / stop.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 <assert.h>
27
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/mi-lttng.h>
30
31 #include "../command.h"
32
33 static char *opt_session_name;
34 static int opt_no_wait;
35 static struct mi_writer *writer;
36
37 #ifdef LTTNG_EMBED_HELP
38 static const char help_msg[] =
39 #include <lttng-stop.1.h>
40 ;
41 #endif
42
43 enum {
44 OPT_HELP = 1,
45 OPT_LIST_OPTIONS,
46 };
47
48 static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
51 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
52 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
53 {0, 0, 0, 0, 0, 0, 0}
54 };
55
56 /*
57 * Mi print of partial session
58 */
59 static int mi_print_session(char *session_name, int enabled)
60 {
61 int ret;
62 assert(writer);
63 assert(session_name);
64
65 /* Open session element */
66 ret = mi_lttng_writer_open_element(writer, config_element_session);
67 if (ret) {
68 goto end;
69 }
70
71 /* Print session name element */
72 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
73 session_name);
74 if (ret) {
75 goto end;
76 }
77
78 /* Is enabled ? */
79 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
80 enabled);
81 if (ret) {
82 goto end;
83 }
84
85 /* Close session element */
86 ret = mi_lttng_writer_close_element(writer);
87
88 end:
89 return ret;
90 }
91
92 /*
93 * Start tracing for all trace of the session.
94 */
95 static int stop_tracing(void)
96 {
97 int ret;
98 char *session_name;
99
100 if (opt_session_name == NULL) {
101 session_name = get_session_name();
102 if (session_name == NULL) {
103 ret = CMD_ERROR;
104 goto error;
105 }
106 } else {
107 session_name = opt_session_name;
108 }
109
110 ret = lttng_stop_tracing_no_wait(session_name);
111 if (ret < 0) {
112 switch (-ret) {
113 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
114 WARN("Tracing already stopped for session %s", session_name);
115 break;
116 default:
117 ERR("%s", lttng_strerror(ret));
118 break;
119 }
120 goto free_name;
121 }
122
123 if (!opt_no_wait) {
124 _MSG("Waiting for data availability");
125 fflush(stdout);
126 do {
127 ret = lttng_data_pending(session_name);
128 if (ret < 0) {
129 /* Return the data available call error. */
130 goto free_name;
131 }
132
133 /*
134 * Data sleep time before retrying (in usec). Don't sleep if the call
135 * returned value indicates availability.
136 */
137 if (ret) {
138 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
139 _MSG(".");
140 fflush(stdout);
141 }
142 } while (ret != 0);
143 MSG("");
144 }
145
146 ret = CMD_SUCCESS;
147
148 print_session_stats(session_name);
149 MSG("Tracing stopped for session %s", session_name);
150 if (lttng_opt_mi) {
151 ret = mi_print_session(session_name, 0);
152 if (ret) {
153 goto free_name;
154 }
155 }
156
157 free_name:
158 if (opt_session_name == NULL) {
159 free(session_name);
160 }
161
162 error:
163 return ret;
164 }
165
166 /*
167 * cmd_stop
168 *
169 * The 'stop <options>' first level command
170 */
171 int cmd_stop(int argc, const char **argv)
172 {
173 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
174 static poptContext pc;
175 const char *leftover = NULL;
176
177 pc = poptGetContext(NULL, argc, argv, long_options, 0);
178 poptReadDefaultConfig(pc, 0);
179
180 while ((opt = poptGetNextOpt(pc)) != -1) {
181 switch (opt) {
182 case OPT_HELP:
183 SHOW_HELP();
184 goto end;
185 case OPT_LIST_OPTIONS:
186 list_cmd_options(stdout, long_options);
187 goto end;
188 default:
189 ret = CMD_UNDEFINED;
190 goto end;
191 }
192 }
193
194 /* Mi check */
195 if (lttng_opt_mi) {
196 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
197 if (!writer) {
198 ret = -LTTNG_ERR_NOMEM;
199 goto end;
200 }
201
202 /* Open command element */
203 ret = mi_lttng_writer_command_open(writer,
204 mi_lttng_element_command_stop);
205 if (ret) {
206 ret = CMD_ERROR;
207 goto end;
208 }
209
210 /* Open output element */
211 ret = mi_lttng_writer_open_element(writer,
212 mi_lttng_element_command_output);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
218 /*
219 * Open sessions element
220 * For validation
221 */
222 ret = mi_lttng_writer_open_element(writer,
223 config_element_sessions);
224 if (ret) {
225 ret = CMD_ERROR;
226 goto end;
227 }
228 }
229
230 opt_session_name = (char*) poptGetArg(pc);
231
232 leftover = poptGetArg(pc);
233 if (leftover) {
234 ERR("Unknown argument: %s", leftover);
235 ret = CMD_ERROR;
236 goto end;
237 }
238
239 command_ret = stop_tracing();
240 if (command_ret) {
241 success = 0;
242 }
243
244 /* Mi closing */
245 if (lttng_opt_mi) {
246 /* Close sessions and output element */
247 ret = mi_lttng_close_multi_element(writer, 2);
248 if (ret) {
249 ret = CMD_ERROR;
250 goto end;
251 }
252
253 /* Success ? */
254 ret = mi_lttng_writer_write_element_bool(writer,
255 mi_lttng_element_command_success, success);
256 if (ret) {
257 ret = CMD_ERROR;
258 goto end;
259 }
260
261 /* Command element close */
262 ret = mi_lttng_writer_command_close(writer);
263 if (ret) {
264 ret = CMD_ERROR;
265 goto end;
266 }
267 }
268
269 end:
270 /* Mi clean-up */
271 if (writer && mi_lttng_writer_destroy(writer)) {
272 /* Preserve original error code */
273 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
274 }
275
276 /* Overwrite ret if an error occurred in stop_tracing() */
277 ret = command_ret ? command_ret : ret;
278
279 poptFreeContext(pc);
280 return ret;
281 }
This page took 0.051693 seconds and 5 git commands to generate.