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