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