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