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