Cleanup: Missing whitespace
[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
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
f3ed775e
DG
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
1cfc0bc8
JRJ
28#include <common/sessiond-comm/sessiond-comm.h>
29#include <common/mi-lttng.h>
30
c399183f 31#include "../command.h"
f3ed775e 32
42224349 33
f3ed775e 34static char *opt_session_name;
1cfc0bc8 35static struct mi_writer *writer;
f3ed775e
DG
36
37enum {
38 OPT_HELP = 1,
679b4943 39 OPT_LIST_OPTIONS,
f3ed775e
DG
40};
41
42static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 45 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
f3ed775e
DG
46 {0, 0, 0, 0, 0, 0, 0}
47};
48
49/*
50 * usage
51 */
52static void usage(FILE *ofp)
53{
32a6298d 54 fprintf(ofp, "usage: lttng start [NAME] [OPTIONS]\n");
f3ed775e
DG
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");
32a6298d 59 fprintf(ofp, "Options:\n");
f3ed775e 60 fprintf(ofp, " -h, --help Show this help\n");
679b4943 61 fprintf(ofp, " --list-options Simple listing of options\n");
f3ed775e
DG
62 fprintf(ofp, "\n");
63}
64
1cfc0bc8
JRJ
65static 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
91end:
92 return ret;
93}
94
f3ed775e
DG
95/*
96 * start_tracing
97 *
98 * Start tracing for all trace of the session.
99 */
100static int start_tracing(void)
101{
ae856491 102 int ret;
f3ed775e
DG
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
6a4f824d 117 ret = lttng_start_tracing(session_name);
f3ed775e 118 if (ret < 0) {
42224349 119 switch (-ret) {
f73fabfd 120 case LTTNG_ERR_TRACE_ALREADY_STARTED:
42224349
DG
121 WARN("Tracing already started for session %s", session_name);
122 break;
123 default:
124 ERR("%s", lttng_strerror(ret));
125 break;
126 }
f3ed775e
DG
127 goto free_name;
128 }
129
ae856491
DG
130 ret = CMD_SUCCESS;
131
f3ed775e 132 MSG("Tracing started for session %s", session_name);
1cfc0bc8
JRJ
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 }
f3ed775e
DG
140
141free_name:
b73d0b29
MD
142 if (opt_session_name == NULL) {
143 free(session_name);
144 }
f3ed775e
DG
145error:
146 return ret;
147}
148
149/*
150 * cmd_start
151 *
152 * The 'start <options>' first level command
153 */
154int cmd_start(int argc, const char **argv)
155{
1cfc0bc8 156 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e
DG
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:
ca1c3607 165 usage(stdout);
f3ed775e 166 goto end;
679b4943
SM
167 case OPT_LIST_OPTIONS:
168 list_cmd_options(stdout, long_options);
679b4943 169 goto end;
f3ed775e
DG
170 default:
171 usage(stderr);
172 ret = CMD_UNDEFINED;
173 goto end;
174 }
175 }
176
177 opt_session_name = (char*) poptGetArg(pc);
178
1cfc0bc8
JRJ
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 }
f3ed775e
DG
244
245end:
1cfc0bc8
JRJ
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;
ca1c3607 254 poptFreeContext(pc);
f3ed775e
DG
255 return ret;
256}
This page took 0.053387 seconds and 4 git commands to generate.