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