Clean-up: run format-cpp on the tree
[lttng-tools.git] / src / bin / lttng / lttng.cpp
CommitLineData
826d496d 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
fac6795d 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
fac6795d 5 *
fac6795d
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
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>
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
fac6795d 27
4fc83d94
PP
28static const char *help_msg =
29#ifdef LTTNG_EMBED_HELP
30#include <lttng.1.h>
31#else
cd9adb8b 32 nullptr
4fc83d94 33#endif
28ab034a 34 ;
4fc83d94 35
fac6795d 36/* Variables */
b53d4e59 37static const char *progname;
92360082
JG
38int opt_no_sessiond;
39char *opt_sessiond_path;
f3ed775e 40
8960e9cd
DG
41char *opt_relayd_path;
42
f3ed775e 43enum {
8960e9cd 44 OPT_RELAYD_PATH,
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
cd9adb8b
JG
51static 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 } };
f3ed775e
DG
63
64/* First level command */
28ab034a
JG
65static 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 },
cd9adb8b 75 { "help", nullptr },
28ab034a
JG
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 },
cd9adb8b 95 { nullptr, nullptr } /* Array closure */
f3ed775e
DG
96};
97
70318c38
SS
98static void version(FILE *ofp)
99{
28ab034a
JG
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);
70318c38
SS
105}
106
c7e35b03
JR
107/*
108 * Find the MI output type enum from a string. This function is for the support
109 * of machine interface output.
110 */
111static 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
865abf65
SM
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 */
132static void list_options(FILE *ofp)
133{
134 int i = 0;
cd9adb8b 135 struct option *option = nullptr;
865abf65
SM
136
137 option = &long_options[i];
cd9adb8b 138 while (option->name != nullptr) {
865abf65
SM
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
7442b2ba 150/*
f3ed775e 151 * clean_exit
96243366 152 */
28ab034a 153__attribute__((noreturn)) static void clean_exit(int code)
96243366 154{
f3ed775e
DG
155 DBG("Clean exit");
156 exit(code);
894be886 157}
96243366 158
894be886 159/*
f3ed775e 160 * sighandler
2ef84c95 161 *
f3ed775e 162 * Signal handler for the daemon
2ef84c95 163 */
f3ed775e 164static void sighandler(int sig)
2ef84c95 165{
f3ed775e 166 switch (sig) {
28ab034a
JG
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;
2ef84c95
DG
174 }
175
f3ed775e 176 return;
2ef84c95
DG
177}
178
179/*
f3ed775e 180 * set_signal_handler
894be886 181 *
f3ed775e 182 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 183 */
cd9adb8b 184static int set_signal_handler()
894be886 185{
f3ed775e
DG
186 int ret = 0;
187 struct sigaction sa;
188 sigset_t sigset;
33a2b854 189
f3ed775e 190 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 191 PERROR("sigemptyset");
33a2b854
DG
192 goto end;
193 }
194
f3ed775e
DG
195 sa.sa_handler = sighandler;
196 sa.sa_mask = sigset;
197 sa.sa_flags = 0;
96243366 198
cd9adb8b 199 if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) {
6f04ed72 200 PERROR("sigaction");
f3ed775e 201 goto end;
894be886
DG
202 }
203
96243366 204end:
57167058
DG
205 return ret;
206}
207
fac6795d 208/*
f3ed775e 209 * handle_command
fac6795d 210 *
f3ed775e
DG
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.
1c9f7941 213 *
f3ed775e
DG
214 * If command not found, return -1
215 * else, return function command error code.
1c9f7941 216 */
f3ed775e 217static int handle_command(int argc, char **argv)
1c9f7941 218{
f3ed775e
DG
219 int i = 0, ret;
220 struct cmd_struct *cmd;
1c9f7941 221
cd9adb8b 222 if (*argv == nullptr) {
f3ed775e 223 ret = CMD_SUCCESS;
47b74d63 224 goto end;
1c9f7941
DG
225 }
226
960afba4
PP
227 /* Special case for help command which needs the commands array */
228 if (strcmp(argv[0], "help") == 0) {
28ab034a 229 ret = cmd_help(argc, (const char **) argv, commands);
960afba4
PP
230 goto end;
231 }
232
f3ed775e 233 cmd = &commands[i];
cd9adb8b 234 while (cmd->name != nullptr) {
f3ed775e
DG
235 /* Find command */
236 if (strcmp(argv[0], cmd->name) == 0) {
28ab034a 237 ret = cmd->func(argc, (const char **) argv);
f3ed775e 238 goto end;
894be886 239 }
f3ed775e
DG
240 i++;
241 cmd = &commands[i];
894be886
DG
242 }
243
f3ed775e 244 /* Command not found */
4ce78777 245 ret = CMD_UNDEFINED;
894be886
DG
246
247end:
f3ed775e 248 return ret;
8548ff30
DG
249}
250
6047170f
JG
251static bool command_exists(const char *command)
252{
253 const struct cmd_struct *cmd = commands;
254 bool exists = false;
255
cd9adb8b 256 while (cmd->name != nullptr) {
6047170f
JG
257 if (!strcmp(command, cmd->name)) {
258 exists = true;
259 goto end;
260 }
261 cmd++;
262 }
263
264end:
265 return exists;
266}
267
cd9adb8b 268static void show_basic_help()
9fb6c2db
PP
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("");
e9711845 275 puts("Recording sessions:");
9fb6c2db 276 puts(" create " CONFIG_CMD_DESCR_CREATE);
511ed4e2 277 puts(" clear " CONFIG_CMD_DESCR_CLEAR);
9fb6c2db
PP
278 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
279 puts(" load " CONFIG_CMD_DESCR_LOAD);
faf04ce7 280 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
9fb6c2db
PP
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("");
484b2a0c 289 puts("Recording event rules:");
9fb6c2db
PP
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("");
e9711845 302 puts("Recording session rotation:");
77927271
JG
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("");
9fb6c2db
PP
307 puts("Resource tracking:");
308 puts(" track " CONFIG_CMD_DESCR_TRACK);
309 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
310 puts("");
484b2a0c
PP
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("");
9fb6c2db 316 puts("Miscellaneous:");
9fb6c2db
PP
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
5b8719f5 327/*
3183dbb0 328 * Parse command line arguments.
5b8719f5 329 *
3183dbb0 330 * Return 0 if OK, else -1
5b8719f5 331 */
f3ed775e 332static int parse_args(int argc, char **argv)
5b8719f5 333{
f3ed775e 334 int opt, ret;
5b8719f5 335
e8fa9fb0 336 if (lttng_is_setuid_setgid()) {
28ab034a
JG
337 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.",
338 argv[0]);
e8fa9fb0
MD
339 clean_exit(EXIT_FAILURE);
340 }
341
f3ed775e 342 if (argc < 2) {
9fb6c2db 343 show_basic_help();
f3ed775e 344 clean_exit(EXIT_FAILURE);
5b8719f5
DG
345 }
346
cd9adb8b 347 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, nullptr)) != -1) {
f3ed775e 348 switch (opt) {
70318c38
SS
349 case 'V':
350 version(stdout);
351 ret = 0;
352 goto end;
f3ed775e 353 case 'h':
4fc83d94 354 ret = utils_show_help(1, "lttng", help_msg);
f67353ba 355 if (ret) {
4fc83d94 356 ERR("Cannot show --help for `lttng`");
f67353ba
PP
357 perror("exec");
358 }
3183dbb0 359 goto end;
f3ed775e 360 case 'v':
849e5b7b
DG
361 /* There is only 3 possible level of verbosity. (-vvv) */
362 if (lttng_opt_verbose < 3) {
363 lttng_opt_verbose += 1;
364 }
f3ed775e
DG
365 break;
366 case 'q':
97e19046 367 lttng_opt_quiet = 1;
f3ed775e 368 break;
c7e35b03
JR
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;
f3ed775e
DG
376 case 'g':
377 lttng_set_tracing_group(optarg);
378 break;
8490ae7b 379 case 'n':
f3ed775e
DG
380 opt_no_sessiond = 1;
381 break;
382 case OPT_SESSION_PATH:
3ddaa633 383 free(opt_sessiond_path);
f3ed775e 384 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
385 if (!opt_sessiond_path) {
386 ret = -1;
387 goto error;
388 }
f3ed775e 389 break;
8960e9cd 390 case OPT_RELAYD_PATH:
3ddaa633 391 free(opt_relayd_path);
8960e9cd 392 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
393 if (!opt_relayd_path) {
394 ret = -1;
395 goto error;
396 }
8960e9cd 397 break;
865abf65
SM
398 case OPT_DUMP_OPTIONS:
399 list_options(stdout);
400 ret = 0;
3183dbb0 401 goto end;
865abf65 402 case OPT_DUMP_COMMANDS:
3c9bd23c 403 list_commands(commands, stdout);
865abf65 404 ret = 0;
3183dbb0 405 goto end;
f3ed775e 406 default:
ae856491 407 ret = 1;
f3ed775e
DG
408 goto error;
409 }
5b8719f5 410 }
fac6795d 411
f3ed775e 412 /* If both options are specified, quiet wins */
97e19046
DG
413 if (lttng_opt_verbose && lttng_opt_quiet) {
414 lttng_opt_verbose = 0;
5b8719f5
DG
415 }
416
f67353ba 417 /* No leftovers, quit */
f3ed775e 418 if ((argc - optind) == 0) {
8005f29a 419 ret = 1;
f3ed775e
DG
420 goto error;
421 }
7442b2ba 422
92360082 423 /*
f3ed775e
DG
424 * Handle leftovers which is a first level command with the trailing
425 * options.
426 */
427 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
428 switch (ret) {
429 case CMD_WARNING:
ae856491 430 case CMD_ERROR:
ae856491
DG
431 break;
432 case CMD_UNDEFINED:
6047170f
JG
433 if (!command_exists(*(argv + optind))) {
434 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
28ab034a 435 *(argv + optind));
6047170f 436 } else {
28ab034a 437 ERR("Unrecognized argument used with \'%s\' command", *(argv + optind));
6047170f 438 }
ae856491
DG
439 break;
440 case CMD_FATAL:
4ce78777 441 case CMD_UNSUPPORTED:
4ce78777 442 break;
ae856491 443 case -1:
ae856491
DG
444 ret = 1;
445 break;
446 case 0:
447 break;
448 default:
42224349
DG
449 if (ret < 0) {
450 ret = -ret;
451 }
ae856491 452 break;
96243366
DG
453 }
454
3183dbb0 455end:
f3ed775e 456error:
ae856491 457 return ret;
fac6795d
DG
458}
459
460/*
5b8719f5 461 * main
fac6795d
DG
462 */
463int main(int argc, char *argv[])
464{
465 int ret;
466
467 progname = argv[0] ? argv[0] : "lttng";
468
5b8719f5
DG
469 ret = set_signal_handler();
470 if (ret < 0) {
87378cf5 471 clean_exit(ret);
5b8719f5
DG
472 }
473
f3ed775e 474 ret = parse_args(argc, argv);
ae856491
DG
475 if (ret != 0) {
476 clean_exit(ret);
1fff1faa 477 }
87378cf5 478
fac6795d
DG
479 return 0;
480}
This page took 0.097203 seconds and 4 git commands to generate.