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