Add --enable-embedded-help option to embed --help messages in binaries
[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
DG
146 static poptContext pc;
147
148 pc = poptGetContext(NULL, argc, argv, long_options, 0);
149 poptReadDefaultConfig(pc, 0);
150
151 while ((opt = poptGetNextOpt(pc)) != -1) {
152 switch (opt) {
153 case OPT_HELP:
4ba92f18 154 SHOW_HELP();
f3ed775e 155 goto end;
679b4943
SM
156 case OPT_LIST_OPTIONS:
157 list_cmd_options(stdout, long_options);
679b4943 158 goto end;
f3ed775e 159 default:
f3ed775e
DG
160 ret = CMD_UNDEFINED;
161 goto end;
162 }
163 }
164
165 opt_session_name = (char*) poptGetArg(pc);
166
1cfc0bc8
JRJ
167 /* Mi check */
168 if (lttng_opt_mi) {
169 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
170 if (!writer) {
171 ret = -LTTNG_ERR_NOMEM;
172 goto end;
173 }
174
175 /* Open command element */
176 ret = mi_lttng_writer_command_open(writer,
177 mi_lttng_element_command_start);
178 if (ret) {
179 ret = CMD_ERROR;
180 goto end;
181 }
182
183 /* Open output element */
184 ret = mi_lttng_writer_open_element(writer,
185 mi_lttng_element_command_output);
186 if (ret) {
187 ret = CMD_ERROR;
188 goto end;
189 }
190
191 /*
192 * Open sessions element
193 * For validation purpose
194 */
195 ret = mi_lttng_writer_open_element(writer,
196 config_element_sessions);
197 if (ret) {
198 ret = CMD_ERROR;
199 goto end;
200 }
201 }
202
203 command_ret = start_tracing();
204 if (command_ret) {
205 success = 0;
206 }
207
208 /* Mi closing */
209 if (lttng_opt_mi) {
210 /* Close sessions and output element */
211 ret = mi_lttng_close_multi_element(writer, 2);
212 if (ret) {
213 ret = CMD_ERROR;
214 goto end;
215 }
216
217 /* Success ? */
218 ret = mi_lttng_writer_write_element_bool(writer,
219 mi_lttng_element_command_success, success);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224
225 /* Command element close */
226 ret = mi_lttng_writer_command_close(writer);
227 if (ret) {
228 ret = CMD_ERROR;
229 goto end;
230 }
231 }
f3ed775e
DG
232
233end:
1cfc0bc8
JRJ
234 /* Mi clean-up */
235 if (writer && mi_lttng_writer_destroy(writer)) {
236 /* Preserve original error code */
237 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
238 }
239
240 /* Overwrite ret if an error occurred with start_tracing */
241 ret = command_ret ? command_ret : ret;
ca1c3607 242 poptFreeContext(pc);
f3ed775e
DG
243 return ret;
244}
This page took 0.052401 seconds and 4 git commands to generate.