actions: list: Add `for_each_action_{const, mutable}()` macros
[lttng-tools.git] / src / bin / lttng / lttng.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "command.hpp"
10 #include "version.hpp"
11
12 #include <common/compat/getenv.hpp>
13 #include <common/error.hpp>
14 #include <common/utils.hpp>
15
16 #include <lttng/lttng.h>
17
18 #include <ctype.h>
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 static const char *help_msg =
29 #ifdef LTTNG_EMBED_HELP
30 #include <lttng.1.h>
31 #else
32 nullptr
33 #endif
34 ;
35
36 /* Variables */
37 static const char *progname;
38 int opt_no_sessiond;
39 char *opt_sessiond_path;
40
41 char *opt_relayd_path;
42
43 enum {
44 OPT_RELAYD_PATH,
45 OPT_SESSION_PATH,
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
48 };
49
50 /* Getopt options. No first level command. */
51 static struct option long_options[] = { { "version", 0, nullptr, 'V' },
52 { "help", 0, nullptr, 'h' },
53 { "group", 1, nullptr, 'g' },
54 { "verbose", 0, nullptr, 'v' },
55 { "quiet", 0, nullptr, 'q' },
56 { "mi", 1, nullptr, 'm' },
57 { "no-sessiond", 0, nullptr, 'n' },
58 { "sessiond-path", 1, nullptr, OPT_SESSION_PATH },
59 { "relayd-path", 1, nullptr, OPT_RELAYD_PATH },
60 { "list-options", 0, nullptr, OPT_DUMP_OPTIONS },
61 { "list-commands", 0, nullptr, OPT_DUMP_COMMANDS },
62 { nullptr, 0, nullptr, 0 } };
63
64 /* First level command */
65 static struct cmd_struct commands[] = {
66 { "add-context", cmd_add_context },
67 { "add-trigger", cmd_add_trigger },
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", nullptr },
76 { "list", cmd_list },
77 { "list-triggers", cmd_list_triggers },
78 { "load", cmd_load },
79 { "metadata", cmd_metadata },
80 { "regenerate", cmd_regenerate },
81 { "remove-trigger", cmd_remove_trigger },
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 { nullptr, nullptr } /* Array closure */
96 };
97
98 static void version(FILE *ofp)
99 {
100 fprintf(ofp,
101 "%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 = nullptr;
136
137 option = &long_options[i];
138 while (option->name != nullptr) {
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 __attribute__((noreturn)) 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()
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, nullptr)) < 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 == nullptr) {
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 != nullptr) {
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 != nullptr) {
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()
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("Recording 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("Recording 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("Recording 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("Triggers:");
312 puts(" add-trigger " CONFIG_CMD_DESCR_ADD_TRIGGER);
313 puts(" remove-trigger " CONFIG_CMD_DESCR_REMOVE_TRIGGER);
314 puts(" list-triggers " CONFIG_CMD_DESCR_LIST_TRIGGERS);
315 puts("");
316 puts("Miscellaneous:");
317 puts(" help " CONFIG_CMD_DESCR_HELP);
318 puts(" version " CONFIG_CMD_DESCR_VERSION);
319 puts(" view " CONFIG_CMD_DESCR_VIEW);
320 puts("");
321 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
322 puts("command COMMAND.");
323 puts("");
324 puts("See `man lttng` for more help with the lttng command.");
325 }
326
327 /*
328 * Parse command line arguments.
329 *
330 * Return 0 if OK, else -1
331 */
332 static int parse_args(int argc, char **argv)
333 {
334 int opt, ret;
335
336 if (lttng_is_setuid_setgid()) {
337 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.",
338 argv[0]);
339 clean_exit(EXIT_FAILURE);
340 }
341
342 if (argc < 2) {
343 show_basic_help();
344 clean_exit(EXIT_FAILURE);
345 }
346
347 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, nullptr)) != -1) {
348 switch (opt) {
349 case 'V':
350 version(stdout);
351 ret = 0;
352 goto end;
353 case 'h':
354 ret = utils_show_help(1, "lttng", help_msg);
355 if (ret) {
356 ERR("Cannot show --help for `lttng`");
357 perror("exec");
358 }
359 goto end;
360 case 'v':
361 /* There is only 3 possible level of verbosity. (-vvv) */
362 if (lttng_opt_verbose < 3) {
363 lttng_opt_verbose += 1;
364 }
365 break;
366 case 'q':
367 lttng_opt_quiet = 1;
368 break;
369 case 'm':
370 lttng_opt_mi = mi_output_type(optarg);
371 if (lttng_opt_mi < 0) {
372 ret = lttng_opt_mi;
373 goto error;
374 }
375 break;
376 case 'g':
377 lttng_set_tracing_group(optarg);
378 break;
379 case 'n':
380 opt_no_sessiond = 1;
381 break;
382 case OPT_SESSION_PATH:
383 free(opt_sessiond_path);
384 opt_sessiond_path = strdup(optarg);
385 if (!opt_sessiond_path) {
386 ret = -1;
387 goto error;
388 }
389 break;
390 case OPT_RELAYD_PATH:
391 free(opt_relayd_path);
392 opt_relayd_path = strdup(optarg);
393 if (!opt_relayd_path) {
394 ret = -1;
395 goto error;
396 }
397 break;
398 case OPT_DUMP_OPTIONS:
399 list_options(stdout);
400 ret = 0;
401 goto end;
402 case OPT_DUMP_COMMANDS:
403 list_commands(commands, stdout);
404 ret = 0;
405 goto end;
406 default:
407 ret = 1;
408 goto error;
409 }
410 }
411
412 /* If both options are specified, quiet wins */
413 if (lttng_opt_verbose && lttng_opt_quiet) {
414 lttng_opt_verbose = 0;
415 }
416
417 /* No leftovers, quit */
418 if ((argc - optind) == 0) {
419 ret = 1;
420 goto error;
421 }
422
423 /*
424 * Handle leftovers which is a first level command with the trailing
425 * options.
426 */
427 ret = handle_command(argc - optind, argv + optind);
428 switch (ret) {
429 case CMD_WARNING:
430 case CMD_ERROR:
431 break;
432 case CMD_UNDEFINED:
433 if (!command_exists(*(argv + optind))) {
434 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
435 *(argv + optind));
436 } else {
437 ERR("Unrecognized argument used with \'%s\' command", *(argv + optind));
438 }
439 break;
440 case CMD_FATAL:
441 case CMD_UNSUPPORTED:
442 break;
443 case -1:
444 ret = 1;
445 break;
446 case 0:
447 break;
448 default:
449 if (ret < 0) {
450 ret = -ret;
451 }
452 break;
453 }
454
455 end:
456 error:
457 return ret;
458 }
459
460 /*
461 * main
462 */
463 int main(int argc, char *argv[])
464 {
465 int ret;
466
467 progname = argv[0] ? argv[0] : "lttng";
468
469 ret = set_signal_handler();
470 if (ret < 0) {
471 clean_exit(ret);
472 }
473
474 ret = parse_args(argc, argv);
475 if (ret != 0) {
476 clean_exit(ret);
477 }
478
479 return 0;
480 }
This page took 0.038648 seconds and 4 git commands to generate.