27e332e07b8b1ee57ed5da2979e4c8b6306e146c
[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 <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 { "remove-trigger", cmd_remove_trigger},
83 { "rotate", cmd_rotate},
84 { "enable-rotation", cmd_enable_rotation},
85 { "disable-rotation", cmd_disable_rotation},
86 { "save", cmd_save},
87 { "set-session", cmd_set_session},
88 { "snapshot", cmd_snapshot},
89 { "start", cmd_start},
90 { "status", cmd_status},
91 { "stop", cmd_stop},
92 { "track", cmd_track},
93 { "untrack", cmd_untrack},
94 { "version", cmd_version},
95 { "view", cmd_view},
96 { NULL, NULL} /* Array closure */
97 };
98
99 static void version(FILE *ofp)
100 {
101 fprintf(ofp, "%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 = NULL;
136
137 option = &long_options[i];
138 while (option->name != NULL) {
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))
154 static void clean_exit(int code)
155 {
156 DBG("Clean exit");
157 exit(code);
158 }
159
160 /*
161 * sighandler
162 *
163 * Signal handler for the daemon
164 */
165 static void sighandler(int sig)
166 {
167 switch (sig) {
168 case SIGTERM:
169 DBG("SIGTERM caught");
170 clean_exit(EXIT_FAILURE);
171 break;
172 default:
173 DBG("Unknown signal %d caught", sig);
174 break;
175 }
176
177 return;
178 }
179
180 /*
181 * set_signal_handler
182 *
183 * Setup signal handler for SIGCHLD and SIGTERM.
184 */
185 static int set_signal_handler(void)
186 {
187 int ret = 0;
188 struct sigaction sa;
189 sigset_t sigset;
190
191 if ((ret = sigemptyset(&sigset)) < 0) {
192 PERROR("sigemptyset");
193 goto end;
194 }
195
196 sa.sa_handler = sighandler;
197 sa.sa_mask = sigset;
198 sa.sa_flags = 0;
199
200 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
201 PERROR("sigaction");
202 goto end;
203 }
204
205 end:
206 return ret;
207 }
208
209 /*
210 * handle_command
211 *
212 * Handle the full argv list of a first level command. Will find the command
213 * in the global commands array and call the function callback associated.
214 *
215 * If command not found, return -1
216 * else, return function command error code.
217 */
218 static int handle_command(int argc, char **argv)
219 {
220 int i = 0, ret;
221 struct cmd_struct *cmd;
222
223 if (*argv == NULL) {
224 ret = CMD_SUCCESS;
225 goto end;
226 }
227
228 /* Special case for help command which needs the commands array */
229 if (strcmp(argv[0], "help") == 0) {
230 ret = cmd_help(argc, (const char**) argv, commands);
231 goto end;
232 }
233
234 cmd = &commands[i];
235 while (cmd->name != NULL) {
236 /* Find command */
237 if (strcmp(argv[0], cmd->name) == 0) {
238 ret = cmd->func(argc, (const char**) argv);
239 goto end;
240 }
241 i++;
242 cmd = &commands[i];
243 }
244
245 /* Command not found */
246 ret = CMD_UNDEFINED;
247
248 end:
249 return ret;
250 }
251
252 static bool command_exists(const char *command)
253 {
254 const struct cmd_struct *cmd = commands;
255 bool exists = false;
256
257 while (cmd->name != NULL) {
258 if (!strcmp(command, cmd->name)) {
259 exists = true;
260 goto end;
261 }
262 cmd++;
263 }
264
265 end:
266 return exists;
267 }
268
269 static void show_basic_help(void)
270 {
271 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
272 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
273 puts("");
274 puts("Available commands:");
275 puts("");
276 puts("Recording sessions:");
277 puts(" create " CONFIG_CMD_DESCR_CREATE);
278 puts(" clear " CONFIG_CMD_DESCR_CLEAR);
279 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
280 puts(" load " CONFIG_CMD_DESCR_LOAD);
281 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
282 puts(" save " CONFIG_CMD_DESCR_SAVE);
283 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
284 puts("");
285 puts("Channels:");
286 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
287 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
288 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
289 puts("");
290 puts("Recording event rules:");
291 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
292 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
293 puts("");
294 puts("Status:");
295 puts(" list " CONFIG_CMD_DESCR_LIST);
296 puts(" status " CONFIG_CMD_DESCR_STATUS);
297 puts("");
298 puts("Control:");
299 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
300 puts(" start " CONFIG_CMD_DESCR_START);
301 puts(" stop " CONFIG_CMD_DESCR_STOP);
302 puts("");
303 puts("Recording session rotation:");
304 puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION);
305 puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION);
306 puts(" rotate " CONFIG_CMD_DESCR_ROTATE);
307 puts("");
308 puts("Resource tracking:");
309 puts(" track " CONFIG_CMD_DESCR_TRACK);
310 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
311 puts("");
312 puts("Triggers:");
313 puts(" add-trigger " CONFIG_CMD_DESCR_ADD_TRIGGER);
314 puts(" remove-trigger " CONFIG_CMD_DESCR_REMOVE_TRIGGER);
315 puts(" list-triggers " CONFIG_CMD_DESCR_LIST_TRIGGERS);
316 puts("");
317 puts("Miscellaneous:");
318 puts(" help " CONFIG_CMD_DESCR_HELP);
319 puts(" version " CONFIG_CMD_DESCR_VERSION);
320 puts(" view " CONFIG_CMD_DESCR_VIEW);
321 puts("");
322 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
323 puts("command COMMAND.");
324 puts("");
325 puts("See `man lttng` for more help with the lttng command.");
326 }
327
328 /*
329 * Parse command line arguments.
330 *
331 * Return 0 if OK, else -1
332 */
333 static int parse_args(int argc, char **argv)
334 {
335 int opt, ret;
336
337 if (lttng_is_setuid_setgid()) {
338 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", 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, NULL)) != -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",
438 *(argv + optind));
439 }
440 break;
441 case CMD_FATAL:
442 case CMD_UNSUPPORTED:
443 break;
444 case -1:
445 ret = 1;
446 break;
447 case 0:
448 break;
449 default:
450 if (ret < 0) {
451 ret = -ret;
452 }
453 break;
454 }
455
456 end:
457 error:
458 return ret;
459 }
460
461
462 /*
463 * main
464 */
465 int main(int argc, char *argv[])
466 {
467 int ret;
468
469 progname = argv[0] ? argv[0] : "lttng";
470
471 ret = set_signal_handler();
472 if (ret < 0) {
473 clean_exit(ret);
474 }
475
476 ret = parse_args(argc, argv);
477 if (ret != 0) {
478 clean_exit(ret);
479 }
480
481 return 0;
482 }
This page took 0.041731 seconds and 3 git commands to generate.