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> | |
21 | ||
1cfc0bc8 | 22 | static struct mi_writer *writer; |
f3ed775e | 23 | |
4fc83d94 PP |
24 | #ifdef LTTNG_EMBED_HELP |
25 | static const char help_msg[] = | |
26 | #include <lttng-start.1.h> | |
28ab034a | 27 | ; |
4fc83d94 PP |
28 | #endif |
29 | ||
f3ed775e DG |
30 | enum { |
31 | OPT_HELP = 1, | |
679b4943 | 32 | OPT_LIST_OPTIONS, |
f3ed775e DG |
33 | }; |
34 | ||
35 | static struct poptOption long_options[] = { | |
36 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
37 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
38 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, | |
39 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } | |
f3ed775e DG |
40 | }; |
41 | ||
1cfc0bc8 JRJ |
42 | static int mi_print_session(char *session_name, int enabled) |
43 | { | |
44 | int ret; | |
45 | ||
46 | /* Open session element */ | |
47 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
48 | if (ret) { | |
49 | goto end; | |
50 | } | |
51 | ||
52 | /* Print session name element */ | |
28ab034a | 53 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, session_name); |
1cfc0bc8 JRJ |
54 | if (ret) { |
55 | goto end; | |
56 | } | |
57 | ||
28ab034a | 58 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); |
1cfc0bc8 JRJ |
59 | if (ret) { |
60 | goto end; | |
61 | } | |
62 | ||
63 | /* Close session element */ | |
64 | ret = mi_lttng_writer_close_element(writer); | |
65 | ||
66 | end: | |
67 | return ret; | |
68 | } | |
69 | ||
f3ed775e DG |
70 | /* |
71 | * start_tracing | |
72 | * | |
73 | * Start tracing for all trace of the session. | |
74 | */ | |
5b915816 | 75 | static int start_tracing(const char *arg_session_name) |
f3ed775e | 76 | { |
ae856491 | 77 | int ret; |
f3ed775e DG |
78 | char *session_name; |
79 | ||
cd9adb8b | 80 | if (arg_session_name == nullptr) { |
f3ed775e | 81 | session_name = get_session_name(); |
5b915816 MJ |
82 | } else { |
83 | session_name = strdup(arg_session_name); | |
cd9adb8b | 84 | if (session_name == nullptr) { |
5b915816 | 85 | PERROR("Failed to copy session name"); |
f3ed775e | 86 | } |
5b915816 MJ |
87 | } |
88 | ||
cd9adb8b | 89 | if (session_name == nullptr) { |
5b915816 MJ |
90 | ret = CMD_ERROR; |
91 | goto error; | |
f3ed775e DG |
92 | } |
93 | ||
94 | DBG("Starting tracing for session %s", session_name); | |
95 | ||
6a4f824d | 96 | ret = lttng_start_tracing(session_name); |
f3ed775e | 97 | if (ret < 0) { |
42224349 | 98 | switch (-ret) { |
f73fabfd | 99 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
42224349 DG |
100 | WARN("Tracing already started for session %s", session_name); |
101 | break; | |
102 | default: | |
103 | ERR("%s", lttng_strerror(ret)); | |
104 | break; | |
105 | } | |
f3ed775e DG |
106 | goto free_name; |
107 | } | |
108 | ||
ae856491 DG |
109 | ret = CMD_SUCCESS; |
110 | ||
f3ed775e | 111 | MSG("Tracing started for session %s", session_name); |
1cfc0bc8 JRJ |
112 | if (lttng_opt_mi) { |
113 | ret = mi_print_session(session_name, 1); | |
114 | if (ret) { | |
115 | ret = CMD_ERROR; | |
116 | goto free_name; | |
117 | } | |
118 | } | |
f3ed775e DG |
119 | |
120 | free_name: | |
5b915816 | 121 | free(session_name); |
f3ed775e DG |
122 | error: |
123 | return ret; | |
124 | } | |
125 | ||
126 | /* | |
127 | * cmd_start | |
128 | * | |
129 | * The 'start <options>' first level command | |
130 | */ | |
131 | int cmd_start(int argc, const char **argv) | |
132 | { | |
1cfc0bc8 | 133 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 134 | static poptContext pc; |
cd9adb8b JG |
135 | const char *arg_session_name = nullptr; |
136 | const char *leftover = nullptr; | |
f3ed775e | 137 | |
cd9adb8b | 138 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
f3ed775e DG |
139 | poptReadDefaultConfig(pc, 0); |
140 | ||
141 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
142 | switch (opt) { | |
143 | case OPT_HELP: | |
4ba92f18 | 144 | SHOW_HELP(); |
f3ed775e | 145 | goto end; |
679b4943 SM |
146 | case OPT_LIST_OPTIONS: |
147 | list_cmd_options(stdout, long_options); | |
679b4943 | 148 | goto end; |
f3ed775e | 149 | default: |
f3ed775e DG |
150 | ret = CMD_UNDEFINED; |
151 | goto end; | |
152 | } | |
153 | } | |
154 | ||
5b915816 | 155 | arg_session_name = poptGetArg(pc); |
f3ed775e | 156 | |
68c7f6e5 JD |
157 | leftover = poptGetArg(pc); |
158 | if (leftover) { | |
159 | ERR("Unknown argument: %s", leftover); | |
160 | ret = CMD_ERROR; | |
161 | goto end; | |
162 | } | |
163 | ||
1cfc0bc8 JRJ |
164 | /* Mi check */ |
165 | if (lttng_opt_mi) { | |
166 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
167 | if (!writer) { | |
168 | ret = -LTTNG_ERR_NOMEM; | |
169 | goto end; | |
170 | } | |
171 | ||
172 | /* Open command element */ | |
28ab034a | 173 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_start); |
1cfc0bc8 JRJ |
174 | if (ret) { |
175 | ret = CMD_ERROR; | |
176 | goto end; | |
177 | } | |
178 | ||
179 | /* Open output element */ | |
28ab034a | 180 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
1cfc0bc8 JRJ |
181 | if (ret) { |
182 | ret = CMD_ERROR; | |
183 | goto end; | |
184 | } | |
185 | ||
186 | /* | |
187 | * Open sessions element | |
188 | * For validation purpose | |
189 | */ | |
28ab034a | 190 | ret = mi_lttng_writer_open_element(writer, config_element_sessions); |
1cfc0bc8 JRJ |
191 | if (ret) { |
192 | ret = CMD_ERROR; | |
193 | goto end; | |
194 | } | |
195 | } | |
196 | ||
5b915816 | 197 | command_ret = start_tracing(arg_session_name); |
1cfc0bc8 JRJ |
198 | if (command_ret) { |
199 | success = 0; | |
200 | } | |
201 | ||
202 | /* Mi closing */ | |
203 | if (lttng_opt_mi) { | |
204 | /* Close sessions and output element */ | |
205 | ret = mi_lttng_close_multi_element(writer, 2); | |
206 | if (ret) { | |
207 | ret = CMD_ERROR; | |
208 | goto end; | |
209 | } | |
210 | ||
211 | /* Success ? */ | |
28ab034a JG |
212 | ret = mi_lttng_writer_write_element_bool( |
213 | writer, mi_lttng_element_command_success, success); | |
1cfc0bc8 JRJ |
214 | if (ret) { |
215 | ret = CMD_ERROR; | |
216 | goto end; | |
217 | } | |
218 | ||
219 | /* Command element close */ | |
220 | ret = mi_lttng_writer_command_close(writer); | |
221 | if (ret) { | |
222 | ret = CMD_ERROR; | |
223 | goto end; | |
224 | } | |
225 | } | |
f3ed775e DG |
226 | |
227 | end: | |
1cfc0bc8 JRJ |
228 | /* Mi clean-up */ |
229 | if (writer && mi_lttng_writer_destroy(writer)) { | |
230 | /* Preserve original error code */ | |
231 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
232 | } | |
233 | ||
234 | /* Overwrite ret if an error occurred with start_tracing */ | |
235 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 236 | poptFreeContext(pc); |
f3ed775e DG |
237 | return ret; |
238 | } |