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