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