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