Commit | Line | Data |
---|---|---|
f3ed775e | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
f3ed775e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
f3ed775e | 5 | * |
f3ed775e DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | #include <common/sessiond-comm/sessiond-comm.hpp> | |
13 | ||
f3ed775e DG |
14 | #include <popt.h> |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <sys/stat.h> | |
19 | #include <sys/types.h> | |
20 | #include <unistd.h> | |
f3ed775e | 21 | |
38ee087f | 22 | static int opt_no_wait; |
e5b83100 | 23 | static struct mi_writer *writer; |
f3ed775e | 24 | |
4fc83d94 PP |
25 | #ifdef LTTNG_EMBED_HELP |
26 | static const char help_msg[] = | |
27 | #include <lttng-stop.1.h> | |
28ab034a | 28 | ; |
4fc83d94 PP |
29 | #endif |
30 | ||
f3ed775e DG |
31 | enum { |
32 | OPT_HELP = 1, | |
679b4943 | 33 | OPT_LIST_OPTIONS, |
f3ed775e DG |
34 | }; |
35 | ||
36 | static struct poptOption long_options[] = { | |
37 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
38 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
39 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, | |
40 | { "no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, nullptr, nullptr }, | |
41 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } | |
f3ed775e DG |
42 | }; |
43 | ||
e5b83100 JRJ |
44 | /* |
45 | * Mi print of partial session | |
46 | */ | |
47 | static int mi_print_session(char *session_name, int enabled) | |
48 | { | |
49 | int ret; | |
a0377dfe FD |
50 | LTTNG_ASSERT(writer); |
51 | LTTNG_ASSERT(session_name); | |
e5b83100 JRJ |
52 | |
53 | /* Open session element */ | |
54 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
55 | if (ret) { | |
56 | goto end; | |
57 | } | |
58 | ||
59 | /* Print session name element */ | |
28ab034a | 60 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name); |
e5b83100 JRJ |
61 | if (ret) { |
62 | goto end; | |
63 | } | |
64 | ||
65 | /* Is enabled ? */ | |
28ab034a | 66 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); |
e5b83100 JRJ |
67 | if (ret) { |
68 | goto end; | |
69 | } | |
70 | ||
71 | /* Close session element */ | |
72 | ret = mi_lttng_writer_close_element(writer); | |
73 | ||
74 | end: | |
75 | return ret; | |
76 | } | |
f3ed775e DG |
77 | |
78 | /* | |
cd80958d | 79 | * Start tracing for all trace of the session. |
f3ed775e | 80 | */ |
5b915816 | 81 | static int stop_tracing(const char *arg_session_name) |
f3ed775e | 82 | { |
ae856491 | 83 | int ret; |
f3ed775e DG |
84 | char *session_name; |
85 | ||
cd9adb8b | 86 | if (arg_session_name == nullptr) { |
f3ed775e | 87 | session_name = get_session_name(); |
5b915816 MJ |
88 | } else { |
89 | session_name = strdup(arg_session_name); | |
cd9adb8b | 90 | if (session_name == nullptr) { |
5b915816 | 91 | PERROR("Failed to copy session name"); |
f3ed775e | 92 | } |
5b915816 MJ |
93 | } |
94 | ||
cd9adb8b | 95 | if (session_name == nullptr) { |
5b915816 MJ |
96 | ret = CMD_ERROR; |
97 | goto error; | |
f3ed775e DG |
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: | |
5b915816 | 148 | free(session_name); |
cd80958d | 149 | |
f3ed775e DG |
150 | error: |
151 | return ret; | |
152 | } | |
153 | ||
154 | /* | |
155 | * cmd_stop | |
156 | * | |
157 | * The 'stop <options>' first level command | |
158 | */ | |
159 | int cmd_stop(int argc, const char **argv) | |
160 | { | |
e5b83100 | 161 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 162 | static poptContext pc; |
cd9adb8b JG |
163 | const char *arg_session_name = nullptr; |
164 | const char *leftover = nullptr; | |
f3ed775e | 165 | |
cd9adb8b | 166 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
f3ed775e DG |
167 | poptReadDefaultConfig(pc, 0); |
168 | ||
169 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
170 | switch (opt) { | |
171 | case OPT_HELP: | |
4ba92f18 | 172 | SHOW_HELP(); |
f3ed775e | 173 | goto end; |
679b4943 SM |
174 | case OPT_LIST_OPTIONS: |
175 | list_cmd_options(stdout, long_options); | |
679b4943 | 176 | goto end; |
f3ed775e | 177 | default: |
f3ed775e DG |
178 | ret = CMD_UNDEFINED; |
179 | goto end; | |
180 | } | |
181 | } | |
182 | ||
e5b83100 JRJ |
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 */ | |
28ab034a | 192 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_stop); |
e5b83100 JRJ |
193 | if (ret) { |
194 | ret = CMD_ERROR; | |
195 | goto end; | |
196 | } | |
197 | ||
198 | /* Open output element */ | |
28ab034a | 199 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
e5b83100 JRJ |
200 | if (ret) { |
201 | ret = CMD_ERROR; | |
202 | goto end; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Open sessions element | |
207 | * For validation | |
208 | */ | |
28ab034a | 209 | ret = mi_lttng_writer_open_element(writer, config_element_sessions); |
e5b83100 JRJ |
210 | if (ret) { |
211 | ret = CMD_ERROR; | |
212 | goto end; | |
213 | } | |
214 | } | |
215 | ||
5b915816 | 216 | arg_session_name = poptGetArg(pc); |
f3ed775e | 217 | |
68c7f6e5 JD |
218 | leftover = poptGetArg(pc); |
219 | if (leftover) { | |
220 | ERR("Unknown argument: %s", leftover); | |
221 | ret = CMD_ERROR; | |
222 | goto end; | |
223 | } | |
224 | ||
5b915816 | 225 | command_ret = stop_tracing(arg_session_name); |
e5b83100 JRJ |
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 ? */ | |
28ab034a JG |
240 | ret = mi_lttng_writer_write_element_bool( |
241 | writer, mi_lttng_element_command_success, success); | |
e5b83100 JRJ |
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 | |
255 | end: | |
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 | } |