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