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> | |
16 | ||
1cfc0bc8 JRJ |
17 | #include <common/sessiond-comm/sessiond-comm.h> |
18 | #include <common/mi-lttng.h> | |
19 | ||
c399183f | 20 | #include "../command.h" |
f3ed775e | 21 | |
42224349 | 22 | |
f3ed775e | 23 | static char *opt_session_name; |
1cfc0bc8 | 24 | static struct mi_writer *writer; |
f3ed775e | 25 | |
4fc83d94 PP |
26 | #ifdef LTTNG_EMBED_HELP |
27 | static const char help_msg[] = | |
28 | #include <lttng-start.1.h> | |
29 | ; | |
30 | #endif | |
31 | ||
f3ed775e DG |
32 | enum { |
33 | OPT_HELP = 1, | |
679b4943 | 34 | OPT_LIST_OPTIONS, |
f3ed775e DG |
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}, | |
679b4943 | 40 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
41 | {0, 0, 0, 0, 0, 0, 0} |
42 | }; | |
43 | ||
1cfc0bc8 JRJ |
44 | static int mi_print_session(char *session_name, int enabled) |
45 | { | |
46 | int ret; | |
47 | ||
48 | /* Open session element */ | |
49 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
50 | if (ret) { | |
51 | goto end; | |
52 | } | |
53 | ||
54 | /* Print session name element */ | |
55 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, | |
56 | session_name); | |
57 | if (ret) { | |
58 | goto end; | |
59 | } | |
60 | ||
61 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, | |
62 | enabled); | |
63 | if (ret) { | |
64 | goto end; | |
65 | } | |
66 | ||
67 | /* Close session element */ | |
68 | ret = mi_lttng_writer_close_element(writer); | |
69 | ||
70 | end: | |
71 | return ret; | |
72 | } | |
73 | ||
f3ed775e DG |
74 | /* |
75 | * start_tracing | |
76 | * | |
77 | * Start tracing for all trace of the session. | |
78 | */ | |
79 | static int start_tracing(void) | |
80 | { | |
ae856491 | 81 | int ret; |
f3ed775e DG |
82 | char *session_name; |
83 | ||
84 | if (opt_session_name == NULL) { | |
85 | session_name = get_session_name(); | |
86 | if (session_name == NULL) { | |
87 | ret = CMD_ERROR; | |
88 | goto error; | |
89 | } | |
90 | } else { | |
91 | session_name = opt_session_name; | |
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: | |
b73d0b29 MD |
121 | if (opt_session_name == NULL) { |
122 | free(session_name); | |
123 | } | |
f3ed775e DG |
124 | error: |
125 | return ret; | |
126 | } | |
127 | ||
128 | /* | |
129 | * cmd_start | |
130 | * | |
131 | * The 'start <options>' first level command | |
132 | */ | |
133 | int cmd_start(int argc, const char **argv) | |
134 | { | |
1cfc0bc8 | 135 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e | 136 | static poptContext pc; |
68c7f6e5 | 137 | const char *leftover = NULL; |
f3ed775e DG |
138 | |
139 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
140 | poptReadDefaultConfig(pc, 0); | |
141 | ||
142 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
143 | switch (opt) { | |
144 | case OPT_HELP: | |
4ba92f18 | 145 | SHOW_HELP(); |
f3ed775e | 146 | goto end; |
679b4943 SM |
147 | case OPT_LIST_OPTIONS: |
148 | list_cmd_options(stdout, long_options); | |
679b4943 | 149 | goto end; |
f3ed775e | 150 | default: |
f3ed775e DG |
151 | ret = CMD_UNDEFINED; |
152 | goto end; | |
153 | } | |
154 | } | |
155 | ||
156 | opt_session_name = (char*) poptGetArg(pc); | |
157 | ||
68c7f6e5 JD |
158 | leftover = poptGetArg(pc); |
159 | if (leftover) { | |
160 | ERR("Unknown argument: %s", leftover); | |
161 | ret = CMD_ERROR; | |
162 | goto end; | |
163 | } | |
164 | ||
1cfc0bc8 JRJ |
165 | /* Mi check */ |
166 | if (lttng_opt_mi) { | |
167 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
168 | if (!writer) { | |
169 | ret = -LTTNG_ERR_NOMEM; | |
170 | goto end; | |
171 | } | |
172 | ||
173 | /* Open command element */ | |
174 | ret = mi_lttng_writer_command_open(writer, | |
175 | mi_lttng_element_command_start); | |
176 | if (ret) { | |
177 | ret = CMD_ERROR; | |
178 | goto end; | |
179 | } | |
180 | ||
181 | /* Open output element */ | |
182 | ret = mi_lttng_writer_open_element(writer, | |
183 | mi_lttng_element_command_output); | |
184 | if (ret) { | |
185 | ret = CMD_ERROR; | |
186 | goto end; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Open sessions element | |
191 | * For validation purpose | |
192 | */ | |
193 | ret = mi_lttng_writer_open_element(writer, | |
194 | config_element_sessions); | |
195 | if (ret) { | |
196 | ret = CMD_ERROR; | |
197 | goto end; | |
198 | } | |
199 | } | |
200 | ||
201 | command_ret = start_tracing(); | |
202 | if (command_ret) { | |
203 | success = 0; | |
204 | } | |
205 | ||
206 | /* Mi closing */ | |
207 | if (lttng_opt_mi) { | |
208 | /* Close sessions and output element */ | |
209 | ret = mi_lttng_close_multi_element(writer, 2); | |
210 | if (ret) { | |
211 | ret = CMD_ERROR; | |
212 | goto end; | |
213 | } | |
214 | ||
215 | /* Success ? */ | |
216 | ret = mi_lttng_writer_write_element_bool(writer, | |
217 | mi_lttng_element_command_success, success); | |
218 | if (ret) { | |
219 | ret = CMD_ERROR; | |
220 | goto end; | |
221 | } | |
222 | ||
223 | /* Command element close */ | |
224 | ret = mi_lttng_writer_command_close(writer); | |
225 | if (ret) { | |
226 | ret = CMD_ERROR; | |
227 | goto end; | |
228 | } | |
229 | } | |
f3ed775e DG |
230 | |
231 | end: | |
1cfc0bc8 JRJ |
232 | /* Mi clean-up */ |
233 | if (writer && mi_lttng_writer_destroy(writer)) { | |
234 | /* Preserve original error code */ | |
235 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
236 | } | |
237 | ||
238 | /* Overwrite ret if an error occurred with start_tracing */ | |
239 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 240 | poptFreeContext(pc); |
f3ed775e DG |
241 | return ret; |
242 | } |