Commit | Line | Data |
---|---|---|
f3ed775e DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
f3ed775e DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
f3ed775e DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
f3ed775e DG |
19 | #include <popt.h> |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
26 | ||
1cfc0bc8 JRJ |
27 | #include <common/sessiond-comm/sessiond-comm.h> |
28 | #include <common/mi-lttng.h> | |
29 | ||
c399183f | 30 | #include "../command.h" |
f3ed775e | 31 | |
42224349 | 32 | |
f3ed775e | 33 | static char *opt_session_name; |
1cfc0bc8 | 34 | static struct mi_writer *writer; |
f3ed775e DG |
35 | |
36 | enum { | |
37 | OPT_HELP = 1, | |
679b4943 | 38 | OPT_LIST_OPTIONS, |
f3ed775e DG |
39 | }; |
40 | ||
41 | static struct poptOption long_options[] = { | |
42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
43 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
679b4943 | 44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
45 | {0, 0, 0, 0, 0, 0, 0} |
46 | }; | |
47 | ||
48 | /* | |
49 | * usage | |
50 | */ | |
51 | static void usage(FILE *ofp) | |
52 | { | |
32a6298d | 53 | fprintf(ofp, "usage: lttng start [NAME] [OPTIONS]\n"); |
f3ed775e DG |
54 | fprintf(ofp, "\n"); |
55 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); | |
56 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); | |
57 | fprintf(ofp, "\n"); | |
32a6298d | 58 | fprintf(ofp, "Options:\n"); |
f3ed775e | 59 | fprintf(ofp, " -h, --help Show this help\n"); |
679b4943 | 60 | fprintf(ofp, " --list-options Simple listing of options\n"); |
f3ed775e DG |
61 | fprintf(ofp, "\n"); |
62 | } | |
63 | ||
1cfc0bc8 JRJ |
64 | static int mi_print_session(char *session_name, int enabled) |
65 | { | |
66 | int ret; | |
67 | ||
68 | /* Open session element */ | |
69 | ret = mi_lttng_writer_open_element(writer, config_element_session); | |
70 | if (ret) { | |
71 | goto end; | |
72 | } | |
73 | ||
74 | /* Print session name element */ | |
75 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, | |
76 | session_name); | |
77 | if (ret) { | |
78 | goto end; | |
79 | } | |
80 | ||
81 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, | |
82 | enabled); | |
83 | if (ret) { | |
84 | goto end; | |
85 | } | |
86 | ||
87 | /* Close session element */ | |
88 | ret = mi_lttng_writer_close_element(writer); | |
89 | ||
90 | end: | |
91 | return ret; | |
92 | } | |
93 | ||
f3ed775e DG |
94 | /* |
95 | * start_tracing | |
96 | * | |
97 | * Start tracing for all trace of the session. | |
98 | */ | |
99 | static int start_tracing(void) | |
100 | { | |
ae856491 | 101 | int ret; |
f3ed775e DG |
102 | char *session_name; |
103 | ||
104 | if (opt_session_name == NULL) { | |
105 | session_name = get_session_name(); | |
106 | if (session_name == NULL) { | |
107 | ret = CMD_ERROR; | |
108 | goto error; | |
109 | } | |
110 | } else { | |
111 | session_name = opt_session_name; | |
112 | } | |
113 | ||
114 | DBG("Starting tracing for session %s", session_name); | |
115 | ||
6a4f824d | 116 | ret = lttng_start_tracing(session_name); |
f3ed775e | 117 | if (ret < 0) { |
42224349 | 118 | switch (-ret) { |
f73fabfd | 119 | case LTTNG_ERR_TRACE_ALREADY_STARTED: |
42224349 DG |
120 | WARN("Tracing already started for session %s", session_name); |
121 | break; | |
122 | default: | |
123 | ERR("%s", lttng_strerror(ret)); | |
124 | break; | |
125 | } | |
f3ed775e DG |
126 | goto free_name; |
127 | } | |
128 | ||
ae856491 DG |
129 | ret = CMD_SUCCESS; |
130 | ||
f3ed775e | 131 | MSG("Tracing started for session %s", session_name); |
1cfc0bc8 JRJ |
132 | if (lttng_opt_mi) { |
133 | ret = mi_print_session(session_name, 1); | |
134 | if (ret) { | |
135 | ret = CMD_ERROR; | |
136 | goto free_name; | |
137 | } | |
138 | } | |
f3ed775e DG |
139 | |
140 | free_name: | |
b73d0b29 MD |
141 | if (opt_session_name == NULL) { |
142 | free(session_name); | |
143 | } | |
f3ed775e DG |
144 | error: |
145 | return ret; | |
146 | } | |
147 | ||
148 | /* | |
149 | * cmd_start | |
150 | * | |
151 | * The 'start <options>' first level command | |
152 | */ | |
153 | int cmd_start(int argc, const char **argv) | |
154 | { | |
1cfc0bc8 | 155 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
f3ed775e DG |
156 | static poptContext pc; |
157 | ||
158 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
159 | poptReadDefaultConfig(pc, 0); | |
160 | ||
161 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
162 | switch (opt) { | |
163 | case OPT_HELP: | |
ca1c3607 | 164 | usage(stdout); |
f3ed775e | 165 | goto end; |
679b4943 SM |
166 | case OPT_LIST_OPTIONS: |
167 | list_cmd_options(stdout, long_options); | |
679b4943 | 168 | goto end; |
f3ed775e DG |
169 | default: |
170 | usage(stderr); | |
171 | ret = CMD_UNDEFINED; | |
172 | goto end; | |
173 | } | |
174 | } | |
175 | ||
176 | opt_session_name = (char*) poptGetArg(pc); | |
177 | ||
1cfc0bc8 JRJ |
178 | /* Mi check */ |
179 | if (lttng_opt_mi) { | |
180 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
181 | if (!writer) { | |
182 | ret = -LTTNG_ERR_NOMEM; | |
183 | goto end; | |
184 | } | |
185 | ||
186 | /* Open command element */ | |
187 | ret = mi_lttng_writer_command_open(writer, | |
188 | mi_lttng_element_command_start); | |
189 | if (ret) { | |
190 | ret = CMD_ERROR; | |
191 | goto end; | |
192 | } | |
193 | ||
194 | /* Open output element */ | |
195 | ret = mi_lttng_writer_open_element(writer, | |
196 | mi_lttng_element_command_output); | |
197 | if (ret) { | |
198 | ret = CMD_ERROR; | |
199 | goto end; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Open sessions element | |
204 | * For validation purpose | |
205 | */ | |
206 | ret = mi_lttng_writer_open_element(writer, | |
207 | config_element_sessions); | |
208 | if (ret) { | |
209 | ret = CMD_ERROR; | |
210 | goto end; | |
211 | } | |
212 | } | |
213 | ||
214 | command_ret = start_tracing(); | |
215 | if (command_ret) { | |
216 | success = 0; | |
217 | } | |
218 | ||
219 | /* Mi closing */ | |
220 | if (lttng_opt_mi) { | |
221 | /* Close sessions and output element */ | |
222 | ret = mi_lttng_close_multi_element(writer, 2); | |
223 | if (ret) { | |
224 | ret = CMD_ERROR; | |
225 | goto end; | |
226 | } | |
227 | ||
228 | /* Success ? */ | |
229 | ret = mi_lttng_writer_write_element_bool(writer, | |
230 | mi_lttng_element_command_success, success); | |
231 | if (ret) { | |
232 | ret = CMD_ERROR; | |
233 | goto end; | |
234 | } | |
235 | ||
236 | /* Command element close */ | |
237 | ret = mi_lttng_writer_command_close(writer); | |
238 | if (ret) { | |
239 | ret = CMD_ERROR; | |
240 | goto end; | |
241 | } | |
242 | } | |
f3ed775e DG |
243 | |
244 | end: | |
1cfc0bc8 JRJ |
245 | /* Mi clean-up */ |
246 | if (writer && mi_lttng_writer_destroy(writer)) { | |
247 | /* Preserve original error code */ | |
248 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
249 | } | |
250 | ||
251 | /* Overwrite ret if an error occurred with start_tracing */ | |
252 | ret = command_ret ? command_ret : ret; | |
ca1c3607 | 253 | poptFreeContext(pc); |
f3ed775e DG |
254 | return ret; |
255 | } |