093a9809ccdfe512150f843356f58c0319a54ed6
[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 try {
238 ret = cmd->func(argc, (const char **) argv);
239 } catch (const std::exception& e) {
240 ERR_FMT("{}", e.what());
241 ret = CMD_ERROR;
242 }
243
244 goto end;
245 }
246 i++;
247 cmd = &commands[i];
248 }
249
250 /* Command not found */
251 ret = CMD_UNDEFINED;
252
253 end:
254 return ret;
255 }
256
257 static bool command_exists(const char *command)
258 {
259 const struct cmd_struct *cmd = commands;
260 bool exists = false;
261
262 while (cmd->name != nullptr) {
263 if (!strcmp(command, cmd->name)) {
264 exists = true;
265 goto end;
266 }
267 cmd++;
268 }
269
270 end:
271 return exists;
272 }
273
274 static void show_basic_help()
275 {
276 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
277 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
278 puts("");
279 puts("Available commands:");
280 puts("");
281 puts("Recording sessions:");
282 puts(" create " CONFIG_CMD_DESCR_CREATE);
283 puts(" clear " CONFIG_CMD_DESCR_CLEAR);
284 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
285 puts(" load " CONFIG_CMD_DESCR_LOAD);
286 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
287 puts(" save " CONFIG_CMD_DESCR_SAVE);
288 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
289 puts("");
290 puts("Channels:");
291 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
292 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
293 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
294 puts("");
295 puts("Recording event rules:");
296 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
297 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
298 puts("");
299 puts("Status:");
300 puts(" list " CONFIG_CMD_DESCR_LIST);
301 puts(" status " CONFIG_CMD_DESCR_STATUS);
302 puts("");
303 puts("Control:");
304 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
305 puts(" start " CONFIG_CMD_DESCR_START);
306 puts(" stop " CONFIG_CMD_DESCR_STOP);
307 puts("");
308 puts("Recording session rotation:");
309 puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION);
310 puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION);
311 puts(" rotate " CONFIG_CMD_DESCR_ROTATE);
312 puts("");
313 puts("Resource tracking:");
314 puts(" track " CONFIG_CMD_DESCR_TRACK);
315 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
316 puts("");
317 puts("Triggers:");
318 puts(" add-trigger " CONFIG_CMD_DESCR_ADD_TRIGGER);
319 puts(" remove-trigger " CONFIG_CMD_DESCR_REMOVE_TRIGGER);
320 puts(" list-triggers " CONFIG_CMD_DESCR_LIST_TRIGGERS);
321 puts("");
322 puts("Miscellaneous:");
323 puts(" help " CONFIG_CMD_DESCR_HELP);
324 puts(" version " CONFIG_CMD_DESCR_VERSION);
325 puts(" view " CONFIG_CMD_DESCR_VIEW);
326 puts("");
327 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
328 puts("command COMMAND.");
329 puts("");
330 puts("See `man lttng` for more help with the lttng command.");
331 }
332
333 /*
334 * Parse command line arguments.
335 *
336 * Return 0 if OK, else -1
337 */
338 static int parse_args(int argc, char **argv)
339 {
340 int opt, ret;
341
342 if (lttng_is_setuid_setgid()) {
343 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.",
344 argv[0]);
345 clean_exit(EXIT_FAILURE);
346 }
347
348 if (argc < 2) {
349 show_basic_help();
350 clean_exit(EXIT_FAILURE);
351 }
352
353 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, nullptr)) != -1) {
354 switch (opt) {
355 case 'V':
356 version(stdout);
357 ret = 0;
358 goto end;
359 case 'h':
360 ret = utils_show_help(1, "lttng", help_msg);
361 if (ret) {
362 ERR("Cannot show --help for `lttng`");
363 perror("exec");
364 }
365 goto end;
366 case 'v':
367 /* There is only 3 possible level of verbosity. (-vvv) */
368 if (lttng_opt_verbose < 3) {
369 lttng_opt_verbose += 1;
370 }
371 break;
372 case 'q':
373 lttng_opt_quiet = 1;
374 break;
375 case 'm':
376 lttng_opt_mi = mi_output_type(optarg);
377 if (lttng_opt_mi < 0) {
378 ret = lttng_opt_mi;
379 goto error;
380 }
381 break;
382 case 'g':
383 lttng_set_tracing_group(optarg);
384 break;
385 case 'n':
386 opt_no_sessiond = 1;
387 break;
388 case OPT_SESSION_PATH:
389 free(opt_sessiond_path);
390 opt_sessiond_path = strdup(optarg);
391 if (!opt_sessiond_path) {
392 ret = -1;
393 goto error;
394 }
395 break;
396 case OPT_RELAYD_PATH:
397 free(opt_relayd_path);
398 opt_relayd_path = strdup(optarg);
399 if (!opt_relayd_path) {
400 ret = -1;
401 goto error;
402 }
403 break;
404 case OPT_DUMP_OPTIONS:
405 list_options(stdout);
406 ret = 0;
407 goto end;
408 case OPT_DUMP_COMMANDS:
409 list_commands(commands, stdout);
410 ret = 0;
411 goto end;
412 default:
413 ret = 1;
414 goto error;
415 }
416 }
417
418 /* If both options are specified, quiet wins */
419 if (lttng_opt_verbose && lttng_opt_quiet) {
420 lttng_opt_verbose = 0;
421 }
422
423 /* No leftovers, quit */
424 if ((argc - optind) == 0) {
425 ret = 1;
426 goto error;
427 }
428
429 /*
430 * Handle leftovers which is a first level command with the trailing
431 * options.
432 */
433 ret = handle_command(argc - optind, argv + optind);
434 switch (ret) {
435 case CMD_WARNING:
436 case CMD_ERROR:
437 break;
438 case CMD_UNDEFINED:
439 if (!command_exists(*(argv + optind))) {
440 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
441 *(argv + optind));
442 } else {
443 ERR("Unrecognized argument used with \'%s\' command", *(argv + optind));
444 }
445 break;
446 case CMD_FATAL:
447 case CMD_UNSUPPORTED:
448 break;
449 case -1:
450 ret = 1;
451 break;
452 case 0:
453 break;
454 default:
455 if (ret < 0) {
456 ret = -ret;
457 }
458 break;
459 }
460
461 end:
462 error:
463 return ret;
464 }
465
466 /*
467 * main
468 */
469 static int _main(int argc, char *argv[])
470 {
471 int ret;
472
473 progname = argv[0] ? argv[0] : "lttng";
474
475 ret = set_signal_handler();
476 if (ret < 0) {
477 clean_exit(ret);
478 }
479
480 ret = parse_args(argc, argv);
481 if (ret != 0) {
482 clean_exit(ret);
483 }
484
485 return 0;
486 }
487
488 int main(int argc, char **argv)
489 {
490 try {
491 return _main(argc, argv);
492 } catch (const std::exception& e) {
493 ERR_FMT("Unhandled exception caught by client: %s", e.what());
494 abort();
495 }
496 }
This page took 0.055596 seconds and 4 git commands to generate.