Introduce "--blocking-timeout" channel parameter
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
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 *
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.
fac6795d
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
730389d9 27#include <ctype.h>
fac6795d 28
5b97ec60 29#include <lttng/lttng.h>
db758600 30#include <common/error.h>
e8fa9fb0 31#include <common/compat/getenv.h>
f67353ba 32#include <common/utils.h>
fac6795d 33
c399183f 34#include "command.h"
fac6795d 35
4fc83d94
PP
36static const char *help_msg =
37#ifdef LTTNG_EMBED_HELP
38#include <lttng.1.h>
39#else
40NULL
41#endif
42;
43
fac6795d
DG
44/* Variables */
45static char *progname;
92360082
JG
46int opt_no_sessiond;
47char *opt_sessiond_path;
f3ed775e 48
8960e9cd
DG
49char *opt_relayd_path;
50
f3ed775e 51enum {
8960e9cd 52 OPT_RELAYD_PATH,
f3ed775e 53 OPT_SESSION_PATH,
865abf65
SM
54 OPT_DUMP_OPTIONS,
55 OPT_DUMP_COMMANDS,
f3ed775e
DG
56};
57
58/* Getopt options. No first level command. */
59static struct option long_options[] = {
70318c38 60 {"version", 0, NULL, 'V'},
f3ed775e
DG
61 {"help", 0, NULL, 'h'},
62 {"group", 1, NULL, 'g'},
63 {"verbose", 0, NULL, 'v'},
64 {"quiet", 0, NULL, 'q'},
c7e35b03 65 {"mi", 1, NULL, 'm'},
8490ae7b 66 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 67 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 68 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
69 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
70 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
71 {NULL, 0, NULL, 0}
72};
73
74/* First level command */
75static struct cmd_struct commands[] = {
3ee1477a 76 { "add-context", cmd_add_context},
f3ed775e
DG
77 { "create", cmd_create},
78 { "destroy", cmd_destroy},
3ee1477a 79 { "disable-channel", cmd_disable_channels},
e953ef25 80 { "disable-event", cmd_disable_events},
d36b8583 81 { "enable-channel", cmd_enable_channels},
3ee1477a
PP
82 { "enable-event", cmd_enable_events},
83 { "help", NULL},
84 { "list", cmd_list},
85 { "load", cmd_load},
86 { "metadata", cmd_metadata},
7712f784 87 { "regenerate", cmd_regenerate},
3ee1477a 88 { "save", cmd_save},
3087ef18 89 { "set-session", cmd_set_session},
57f272ed 90 { "snapshot", cmd_snapshot},
3ee1477a
PP
91 { "start", cmd_start},
92 { "status", cmd_status},
93 { "stop", cmd_stop},
ccf10263
MD
94 { "track", cmd_track},
95 { "untrack", cmd_untrack},
3ee1477a
PP
96 { "version", cmd_version},
97 { "view", cmd_view},
f3ed775e
DG
98 { NULL, NULL} /* Array closure */
99};
100
70318c38
SS
101static void version(FILE *ofp)
102{
4c6ac053
MD
103 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
104 progname,
105 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
106}
107
c7e35b03
JR
108/*
109 * Find the MI output type enum from a string. This function is for the support
110 * of machine interface output.
111 */
112static int mi_output_type(const char *output_type)
113{
114 int ret = 0;
115
116 if (!strncasecmp("xml", output_type, 3)) {
117 ret = LTTNG_MI_XML;
118 } else {
119 /* Invalid output format */
120 ERR("MI output format not supported");
121 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
122 }
123
124 return ret;
125}
126
865abf65
SM
127/*
128 * list_options
129 *
130 * List options line by line. This is mostly for bash auto completion and to
131 * avoid difficult parsing.
132 */
133static void list_options(FILE *ofp)
134{
135 int i = 0;
136 struct option *option = NULL;
137
138 option = &long_options[i];
139 while (option->name != NULL) {
140 fprintf(ofp, "--%s\n", option->name);
141
142 if (isprint(option->val)) {
143 fprintf(ofp, "-%c\n", option->val);
144 }
145
146 i++;
147 option = &long_options[i];
148 }
149}
150
7442b2ba 151/*
f3ed775e 152 * clean_exit
96243366 153 */
f3ed775e 154static void clean_exit(int code)
96243366 155{
f3ed775e
DG
156 DBG("Clean exit");
157 exit(code);
894be886 158}
96243366 159
894be886 160/*
f3ed775e 161 * sighandler
2ef84c95 162 *
f3ed775e 163 * Signal handler for the daemon
2ef84c95 164 */
f3ed775e 165static void sighandler(int sig)
2ef84c95 166{
f3ed775e
DG
167 switch (sig) {
168 case SIGTERM:
99bab54f 169 DBG("SIGTERM caught");
f3ed775e
DG
170 clean_exit(EXIT_FAILURE);
171 break;
f3ed775e 172 default:
99bab54f 173 DBG("Unknown signal %d caught", sig);
f3ed775e 174 break;
2ef84c95
DG
175 }
176
f3ed775e 177 return;
2ef84c95
DG
178}
179
180/*
f3ed775e 181 * set_signal_handler
894be886 182 *
f3ed775e 183 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 184 */
f3ed775e 185static int set_signal_handler(void)
894be886 186{
f3ed775e
DG
187 int ret = 0;
188 struct sigaction sa;
189 sigset_t sigset;
33a2b854 190
f3ed775e 191 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 192 PERROR("sigemptyset");
33a2b854
DG
193 goto end;
194 }
195
f3ed775e
DG
196 sa.sa_handler = sighandler;
197 sa.sa_mask = sigset;
198 sa.sa_flags = 0;
96243366 199
f3ed775e 200 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 201 PERROR("sigaction");
f3ed775e 202 goto end;
894be886
DG
203 }
204
96243366 205end:
57167058
DG
206 return ret;
207}
208
fac6795d 209/*
f3ed775e 210 * handle_command
fac6795d 211 *
f3ed775e
DG
212 * Handle the full argv list of a first level command. Will find the command
213 * in the global commands array and call the function callback associated.
1c9f7941 214 *
f3ed775e
DG
215 * If command not found, return -1
216 * else, return function command error code.
1c9f7941 217 */
f3ed775e 218static int handle_command(int argc, char **argv)
1c9f7941 219{
f3ed775e
DG
220 int i = 0, ret;
221 struct cmd_struct *cmd;
1c9f7941 222
f3ed775e
DG
223 if (*argv == NULL) {
224 ret = CMD_SUCCESS;
47b74d63 225 goto end;
1c9f7941
DG
226 }
227
960afba4
PP
228 /* Special case for help command which needs the commands array */
229 if (strcmp(argv[0], "help") == 0) {
230 ret = cmd_help(argc, (const char**) argv, commands);
231 goto end;
232 }
233
f3ed775e 234 cmd = &commands[i];
960afba4 235 while (cmd->name != NULL) {
f3ed775e
DG
236 /* Find command */
237 if (strcmp(argv[0], cmd->name) == 0) {
238 ret = cmd->func(argc, (const char**) argv);
f3ed775e 239 goto end;
894be886 240 }
f3ed775e
DG
241 i++;
242 cmd = &commands[i];
894be886
DG
243 }
244
f3ed775e 245 /* Command not found */
4ce78777 246 ret = CMD_UNDEFINED;
894be886
DG
247
248end:
f3ed775e 249 return ret;
8548ff30
DG
250}
251
9fb6c2db
PP
252static void show_basic_help(void)
253{
254 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
255 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
256 puts("");
257 puts("Available commands:");
258 puts("");
259 puts("Tracing sessions:");
260 puts(" create " CONFIG_CMD_DESCR_CREATE);
261 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
262 puts(" load " CONFIG_CMD_DESCR_LOAD);
faf04ce7 263 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
9fb6c2db
PP
264 puts(" save " CONFIG_CMD_DESCR_SAVE);
265 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
266 puts("");
267 puts("Channels:");
268 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
269 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
270 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
271 puts("");
272 puts("Event rules:");
273 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
274 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
275 puts("");
276 puts("Status:");
277 puts(" list " CONFIG_CMD_DESCR_LIST);
278 puts(" status " CONFIG_CMD_DESCR_STATUS);
279 puts("");
280 puts("Control:");
281 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
282 puts(" start " CONFIG_CMD_DESCR_START);
283 puts(" stop " CONFIG_CMD_DESCR_STOP);
284 puts("");
285 puts("Resource tracking:");
286 puts(" track " CONFIG_CMD_DESCR_TRACK);
287 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
288 puts("");
289 puts("Miscellaneous:");
9fb6c2db
PP
290 puts(" help " CONFIG_CMD_DESCR_HELP);
291 puts(" version " CONFIG_CMD_DESCR_VERSION);
292 puts(" view " CONFIG_CMD_DESCR_VIEW);
293 puts("");
294 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
295 puts("command COMMAND.");
296 puts("");
297 puts("See `man lttng` for more help with the lttng command.");
298}
299
5b8719f5 300/*
3183dbb0 301 * Parse command line arguments.
5b8719f5 302 *
3183dbb0 303 * Return 0 if OK, else -1
5b8719f5 304 */
f3ed775e 305static int parse_args(int argc, char **argv)
5b8719f5 306{
f3ed775e 307 int opt, ret;
5b8719f5 308
e8fa9fb0
MD
309 if (lttng_is_setuid_setgid()) {
310 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
311 clean_exit(EXIT_FAILURE);
312 }
313
f3ed775e 314 if (argc < 2) {
9fb6c2db 315 show_basic_help();
f3ed775e 316 clean_exit(EXIT_FAILURE);
5b8719f5
DG
317 }
318
c7e35b03 319 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 320 switch (opt) {
70318c38
SS
321 case 'V':
322 version(stdout);
323 ret = 0;
324 goto end;
f3ed775e 325 case 'h':
4fc83d94 326 ret = utils_show_help(1, "lttng", help_msg);
f67353ba 327 if (ret) {
4fc83d94 328 ERR("Cannot show --help for `lttng`");
f67353ba
PP
329 perror("exec");
330 }
3183dbb0 331 goto end;
f3ed775e 332 case 'v':
849e5b7b
DG
333 /* There is only 3 possible level of verbosity. (-vvv) */
334 if (lttng_opt_verbose < 3) {
335 lttng_opt_verbose += 1;
336 }
f3ed775e
DG
337 break;
338 case 'q':
97e19046 339 lttng_opt_quiet = 1;
f3ed775e 340 break;
c7e35b03
JR
341 case 'm':
342 lttng_opt_mi = mi_output_type(optarg);
343 if (lttng_opt_mi < 0) {
344 ret = lttng_opt_mi;
345 goto error;
346 }
347 break;
f3ed775e
DG
348 case 'g':
349 lttng_set_tracing_group(optarg);
350 break;
8490ae7b 351 case 'n':
f3ed775e
DG
352 opt_no_sessiond = 1;
353 break;
354 case OPT_SESSION_PATH:
3ddaa633 355 free(opt_sessiond_path);
f3ed775e 356 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
357 if (!opt_sessiond_path) {
358 ret = -1;
359 goto error;
360 }
f3ed775e 361 break;
8960e9cd 362 case OPT_RELAYD_PATH:
3ddaa633 363 free(opt_relayd_path);
8960e9cd 364 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
365 if (!opt_relayd_path) {
366 ret = -1;
367 goto error;
368 }
8960e9cd 369 break;
865abf65
SM
370 case OPT_DUMP_OPTIONS:
371 list_options(stdout);
372 ret = 0;
3183dbb0 373 goto end;
865abf65 374 case OPT_DUMP_COMMANDS:
3c9bd23c 375 list_commands(commands, stdout);
865abf65 376 ret = 0;
3183dbb0 377 goto end;
f3ed775e 378 default:
ae856491 379 ret = 1;
f3ed775e
DG
380 goto error;
381 }
5b8719f5 382 }
fac6795d 383
f3ed775e 384 /* If both options are specified, quiet wins */
97e19046
DG
385 if (lttng_opt_verbose && lttng_opt_quiet) {
386 lttng_opt_verbose = 0;
5b8719f5
DG
387 }
388
f67353ba 389 /* No leftovers, quit */
f3ed775e 390 if ((argc - optind) == 0) {
8005f29a 391 ret = 1;
f3ed775e
DG
392 goto error;
393 }
7442b2ba 394
92360082 395 /*
f3ed775e
DG
396 * Handle leftovers which is a first level command with the trailing
397 * options.
398 */
399 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
400 switch (ret) {
401 case CMD_WARNING:
402 WARN("Some command(s) went wrong");
403 break;
404 case CMD_ERROR:
405 ERR("Command error");
406 break;
407 case CMD_UNDEFINED:
f0f2dce9 408 ERR("Undefined command or invalid arguments");
ae856491
DG
409 break;
410 case CMD_FATAL:
411 ERR("Fatal error");
412 break;
4ce78777
DG
413 case CMD_UNSUPPORTED:
414 ERR("Unsupported command");
415 break;
ae856491 416 case -1:
ae856491
DG
417 ret = 1;
418 break;
419 case 0:
420 break;
421 default:
42224349
DG
422 if (ret < 0) {
423 ret = -ret;
424 }
ae856491 425 break;
96243366
DG
426 }
427
3183dbb0 428end:
f3ed775e 429error:
ae856491 430 return ret;
fac6795d
DG
431}
432
f3ed775e 433
fac6795d 434/*
5b8719f5 435 * main
fac6795d
DG
436 */
437int main(int argc, char *argv[])
438{
439 int ret;
440
441 progname = argv[0] ? argv[0] : "lttng";
442
5b8719f5
DG
443 ret = set_signal_handler();
444 if (ret < 0) {
87378cf5 445 clean_exit(ret);
5b8719f5
DG
446 }
447
f3ed775e 448 ret = parse_args(argc, argv);
ae856491
DG
449 if (ret != 0) {
450 clean_exit(ret);
1fff1faa 451 }
87378cf5 452
fac6795d
DG
453 return 0;
454}
This page took 0.071593 seconds and 4 git commands to generate.