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