Kernel ioctl to regenerate the statedump
[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
6c1c0768 18#define _LGPL_SOURCE
f3ed775e
DG
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
e5b83100
JRJ
50/*
51 * Mi print of partial session
52 */
53static int mi_print_session(char *session_name, int enabled)
54{
55 int ret;
56 assert(writer);
57 assert(session_name);
58
59 /* Open session element */
60 ret = mi_lttng_writer_open_element(writer, config_element_session);
61 if (ret) {
62 goto end;
63 }
64
65 /* Print session name element */
66 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
67 session_name);
68 if (ret) {
69 goto end;
70 }
71
72 /* Is enabled ? */
73 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
74 enabled);
75 if (ret) {
76 goto end;
77 }
78
79 /* Close session element */
80 ret = mi_lttng_writer_close_element(writer);
81
82end:
83 return ret;
84}
f3ed775e
DG
85
86/*
cd80958d 87 * Start tracing for all trace of the session.
f3ed775e
DG
88 */
89static int stop_tracing(void)
90{
ae856491 91 int ret;
f3ed775e
DG
92 char *session_name;
93
94 if (opt_session_name == NULL) {
95 session_name = get_session_name();
96 if (session_name == NULL) {
97 ret = CMD_ERROR;
98 goto error;
99 }
100 } else {
101 session_name = opt_session_name;
102 }
103
8eb7a5e2 104 ret = lttng_stop_tracing_no_wait(session_name);
f3ed775e 105 if (ret < 0) {
42224349 106 switch (-ret) {
f73fabfd 107 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
42224349
DG
108 WARN("Tracing already stopped for session %s", session_name);
109 break;
110 default:
111 ERR("%s", lttng_strerror(ret));
112 break;
113 }
f3ed775e
DG
114 goto free_name;
115 }
116
8eb7a5e2
JG
117 if (!opt_no_wait) {
118 _MSG("Waiting for data availability");
119 fflush(stdout);
120 do {
121 ret = lttng_data_pending(session_name);
122 if (ret < 0) {
123 /* Return the data available call error. */
e5b83100 124 goto free_name;
8eb7a5e2
JG
125 }
126
127 /*
128 * Data sleep time before retrying (in usec). Don't sleep if the call
129 * returned value indicates availability.
130 */
131 if (ret) {
132 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
133 _MSG(".");
134 fflush(stdout);
135 }
136 } while (ret != 0);
137 MSG("");
138 }
139
ae856491
DG
140 ret = CMD_SUCCESS;
141
20fb9e02 142 print_session_stats(session_name);
f3ed775e 143 MSG("Tracing stopped for session %s", session_name);
e5b83100
JRJ
144 if (lttng_opt_mi) {
145 ret = mi_print_session(session_name, 0);
146 if (ret) {
147 goto free_name;
148 }
149 }
f3ed775e
DG
150
151free_name:
b73d0b29
MD
152 if (opt_session_name == NULL) {
153 free(session_name);
154 }
cd80958d 155
f3ed775e
DG
156error:
157 return ret;
158}
159
160/*
161 * cmd_stop
162 *
163 * The 'stop <options>' first level command
164 */
165int cmd_stop(int argc, const char **argv)
166{
e5b83100 167 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e
DG
168 static poptContext pc;
169
170 pc = poptGetContext(NULL, argc, argv, long_options, 0);
171 poptReadDefaultConfig(pc, 0);
172
173 while ((opt = poptGetNextOpt(pc)) != -1) {
174 switch (opt) {
175 case OPT_HELP:
4ba92f18 176 SHOW_HELP();
f3ed775e 177 goto end;
679b4943
SM
178 case OPT_LIST_OPTIONS:
179 list_cmd_options(stdout, long_options);
679b4943 180 goto end;
f3ed775e 181 default:
f3ed775e
DG
182 ret = CMD_UNDEFINED;
183 goto end;
184 }
185 }
186
e5b83100
JRJ
187 /* Mi check */
188 if (lttng_opt_mi) {
189 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
190 if (!writer) {
191 ret = -LTTNG_ERR_NOMEM;
192 goto end;
193 }
194
195 /* Open command element */
196 ret = mi_lttng_writer_command_open(writer,
197 mi_lttng_element_command_stop);
198 if (ret) {
199 ret = CMD_ERROR;
200 goto end;
201 }
202
203 /* Open output element */
204 ret = mi_lttng_writer_open_element(writer,
205 mi_lttng_element_command_output);
206 if (ret) {
207 ret = CMD_ERROR;
208 goto end;
209 }
210
211 /*
212 * Open sessions element
213 * For validation
214 */
215 ret = mi_lttng_writer_open_element(writer,
216 config_element_sessions);
217 if (ret) {
218 ret = CMD_ERROR;
219 goto end;
220 }
221 }
222
f3ed775e
DG
223 opt_session_name = (char*) poptGetArg(pc);
224
e5b83100
JRJ
225 command_ret = stop_tracing();
226 if (command_ret) {
227 success = 0;
228 }
229
230 /* Mi closing */
231 if (lttng_opt_mi) {
232 /* Close sessions and output element */
233 ret = mi_lttng_close_multi_element(writer, 2);
234 if (ret) {
235 ret = CMD_ERROR;
236 goto end;
237 }
238
239 /* Success ? */
240 ret = mi_lttng_writer_write_element_bool(writer,
241 mi_lttng_element_command_success, success);
242 if (ret) {
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 /* Command element close */
248 ret = mi_lttng_writer_command_close(writer);
249 if (ret) {
250 ret = CMD_ERROR;
251 goto end;
252 }
253 }
f3ed775e
DG
254
255end:
e5b83100
JRJ
256 /* Mi clean-up */
257 if (writer && mi_lttng_writer_destroy(writer)) {
258 /* Preserve original error code */
259 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
260 }
261
262 /* Overwrite ret if an error occurred in stop_tracing() */
263 ret = command_ret ? command_ret : ret;
264
ca1c3607 265 poptFreeContext(pc);
f3ed775e
DG
266 return ret;
267}
This page took 0.054447 seconds and 4 git commands to generate.