Add UST namespace contexts
[lttng-tools.git] / src / bin / lttng / lttng.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
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 #include <ctype.h>
28
29 #include <lttng/lttng.h>
30 #include <common/error.h>
31 #include <common/compat/getenv.h>
32 #include <common/utils.h>
33
34 #include "command.h"
35 #include "version.h"
36
37 static const char *help_msg =
38 #ifdef LTTNG_EMBED_HELP
39 #include <lttng.1.h>
40 #else
41 NULL
42 #endif
43 ;
44
45 /* Variables */
46 static char *progname;
47 int opt_no_sessiond;
48 char *opt_sessiond_path;
49
50 char *opt_relayd_path;
51
52 enum {
53 OPT_RELAYD_PATH,
54 OPT_SESSION_PATH,
55 OPT_DUMP_OPTIONS,
56 OPT_DUMP_COMMANDS,
57 };
58
59 /* Getopt options. No first level command. */
60 static struct option long_options[] = {
61 {"version", 0, NULL, 'V'},
62 {"help", 0, NULL, 'h'},
63 {"group", 1, NULL, 'g'},
64 {"verbose", 0, NULL, 'v'},
65 {"quiet", 0, NULL, 'q'},
66 {"mi", 1, NULL, 'm'},
67 {"no-sessiond", 0, NULL, 'n'},
68 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
69 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
70 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
71 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
72 {NULL, 0, NULL, 0}
73 };
74
75 /* First level command */
76 static struct cmd_struct commands[] = {
77 { "add-context", cmd_add_context},
78 { "create", cmd_create},
79 { "destroy", cmd_destroy},
80 { "disable-channel", cmd_disable_channels},
81 { "disable-event", cmd_disable_events},
82 { "enable-channel", cmd_enable_channels},
83 { "enable-event", cmd_enable_events},
84 { "help", NULL},
85 { "list", cmd_list},
86 { "load", cmd_load},
87 { "metadata", cmd_metadata},
88 { "regenerate", cmd_regenerate},
89 { "rotate", cmd_rotate},
90 { "enable-rotation", cmd_enable_rotation},
91 { "disable-rotation", cmd_disable_rotation},
92 { "save", cmd_save},
93 { "set-session", cmd_set_session},
94 { "snapshot", cmd_snapshot},
95 { "start", cmd_start},
96 { "status", cmd_status},
97 { "stop", cmd_stop},
98 { "track", cmd_track},
99 { "untrack", cmd_untrack},
100 { "version", cmd_version},
101 { "view", cmd_view},
102 { NULL, NULL} /* Array closure */
103 };
104
105 static void version(FILE *ofp)
106 {
107 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s%s\n",
108 progname,
109 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
110 EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
111 }
112
113 /*
114 * Find the MI output type enum from a string. This function is for the support
115 * of machine interface output.
116 */
117 static int mi_output_type(const char *output_type)
118 {
119 int ret = 0;
120
121 if (!strncasecmp("xml", output_type, 3)) {
122 ret = LTTNG_MI_XML;
123 } else {
124 /* Invalid output format */
125 ERR("MI output format not supported");
126 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
127 }
128
129 return ret;
130 }
131
132 /*
133 * list_options
134 *
135 * List options line by line. This is mostly for bash auto completion and to
136 * avoid difficult parsing.
137 */
138 static void list_options(FILE *ofp)
139 {
140 int i = 0;
141 struct option *option = NULL;
142
143 option = &long_options[i];
144 while (option->name != NULL) {
145 fprintf(ofp, "--%s\n", option->name);
146
147 if (isprint(option->val)) {
148 fprintf(ofp, "-%c\n", option->val);
149 }
150
151 i++;
152 option = &long_options[i];
153 }
154 }
155
156 /*
157 * clean_exit
158 */
159 static void clean_exit(int code)
160 {
161 DBG("Clean exit");
162 exit(code);
163 }
164
165 /*
166 * sighandler
167 *
168 * Signal handler for the daemon
169 */
170 static void sighandler(int sig)
171 {
172 switch (sig) {
173 case SIGTERM:
174 DBG("SIGTERM caught");
175 clean_exit(EXIT_FAILURE);
176 break;
177 default:
178 DBG("Unknown signal %d caught", sig);
179 break;
180 }
181
182 return;
183 }
184
185 /*
186 * set_signal_handler
187 *
188 * Setup signal handler for SIGCHLD and SIGTERM.
189 */
190 static int set_signal_handler(void)
191 {
192 int ret = 0;
193 struct sigaction sa;
194 sigset_t sigset;
195
196 if ((ret = sigemptyset(&sigset)) < 0) {
197 PERROR("sigemptyset");
198 goto end;
199 }
200
201 sa.sa_handler = sighandler;
202 sa.sa_mask = sigset;
203 sa.sa_flags = 0;
204
205 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
206 PERROR("sigaction");
207 goto end;
208 }
209
210 end:
211 return ret;
212 }
213
214 /*
215 * handle_command
216 *
217 * Handle the full argv list of a first level command. Will find the command
218 * in the global commands array and call the function callback associated.
219 *
220 * If command not found, return -1
221 * else, return function command error code.
222 */
223 static int handle_command(int argc, char **argv)
224 {
225 int i = 0, ret;
226 struct cmd_struct *cmd;
227
228 if (*argv == NULL) {
229 ret = CMD_SUCCESS;
230 goto end;
231 }
232
233 /* Special case for help command which needs the commands array */
234 if (strcmp(argv[0], "help") == 0) {
235 ret = cmd_help(argc, (const char**) argv, commands);
236 goto end;
237 }
238
239 cmd = &commands[i];
240 while (cmd->name != NULL) {
241 /* Find command */
242 if (strcmp(argv[0], cmd->name) == 0) {
243 ret = cmd->func(argc, (const char**) argv);
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 != NULL) {
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(void)
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("Tracing sessions:");
282 puts(" create " CONFIG_CMD_DESCR_CREATE);
283 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
284 puts(" load " CONFIG_CMD_DESCR_LOAD);
285 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
286 puts(" save " CONFIG_CMD_DESCR_SAVE);
287 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
288 puts("");
289 puts("Channels:");
290 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
291 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
292 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
293 puts("");
294 puts("Event rules:");
295 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
296 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
297 puts("");
298 puts("Status:");
299 puts(" list " CONFIG_CMD_DESCR_LIST);
300 puts(" status " CONFIG_CMD_DESCR_STATUS);
301 puts("");
302 puts("Control:");
303 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
304 puts(" start " CONFIG_CMD_DESCR_START);
305 puts(" stop " CONFIG_CMD_DESCR_STOP);
306 puts("");
307 puts("Tracing session rotation:");
308 puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION);
309 puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION);
310 puts(" rotate " CONFIG_CMD_DESCR_ROTATE);
311 puts("");
312 puts("Resource tracking:");
313 puts(" track " CONFIG_CMD_DESCR_TRACK);
314 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
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.", argv[0]);
338 clean_exit(EXIT_FAILURE);
339 }
340
341 if (argc < 2) {
342 show_basic_help();
343 clean_exit(EXIT_FAILURE);
344 }
345
346 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
347 switch (opt) {
348 case 'V':
349 version(stdout);
350 ret = 0;
351 goto end;
352 case 'h':
353 ret = utils_show_help(1, "lttng", help_msg);
354 if (ret) {
355 ERR("Cannot show --help for `lttng`");
356 perror("exec");
357 }
358 goto end;
359 case 'v':
360 /* There is only 3 possible level of verbosity. (-vvv) */
361 if (lttng_opt_verbose < 3) {
362 lttng_opt_verbose += 1;
363 }
364 break;
365 case 'q':
366 lttng_opt_quiet = 1;
367 break;
368 case 'm':
369 lttng_opt_mi = mi_output_type(optarg);
370 if (lttng_opt_mi < 0) {
371 ret = lttng_opt_mi;
372 goto error;
373 }
374 break;
375 case 'g':
376 lttng_set_tracing_group(optarg);
377 break;
378 case 'n':
379 opt_no_sessiond = 1;
380 break;
381 case OPT_SESSION_PATH:
382 free(opt_sessiond_path);
383 opt_sessiond_path = strdup(optarg);
384 if (!opt_sessiond_path) {
385 ret = -1;
386 goto error;
387 }
388 break;
389 case OPT_RELAYD_PATH:
390 free(opt_relayd_path);
391 opt_relayd_path = strdup(optarg);
392 if (!opt_relayd_path) {
393 ret = -1;
394 goto error;
395 }
396 break;
397 case OPT_DUMP_OPTIONS:
398 list_options(stdout);
399 ret = 0;
400 goto end;
401 case OPT_DUMP_COMMANDS:
402 list_commands(commands, stdout);
403 ret = 0;
404 goto end;
405 default:
406 ret = 1;
407 goto error;
408 }
409 }
410
411 /* If both options are specified, quiet wins */
412 if (lttng_opt_verbose && lttng_opt_quiet) {
413 lttng_opt_verbose = 0;
414 }
415
416 /* No leftovers, quit */
417 if ((argc - optind) == 0) {
418 ret = 1;
419 goto error;
420 }
421
422 /*
423 * Handle leftovers which is a first level command with the trailing
424 * options.
425 */
426 ret = handle_command(argc - optind, argv + optind);
427 switch (ret) {
428 case CMD_WARNING:
429 case CMD_ERROR:
430 break;
431 case CMD_UNDEFINED:
432 if (!command_exists(*(argv + optind))) {
433 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
434 *(argv + optind));
435 } else {
436 ERR("Unrecognized argument used with \'%s\' command",
437 *(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 /*
462 * main
463 */
464 int main(int argc, char *argv[])
465 {
466 int ret;
467
468 progname = argv[0] ? argv[0] : "lttng";
469
470 ret = set_signal_handler();
471 if (ret < 0) {
472 clean_exit(ret);
473 }
474
475 ret = parse_args(argc, argv);
476 if (ret != 0) {
477 clean_exit(ret);
478 }
479
480 return 0;
481 }
This page took 0.041172 seconds and 4 git commands to generate.