Prettify channel listing
[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
36 static const char *help_msg =
37 #ifdef LTTNG_EMBED_HELP
38 #include <lttng.1.h>
39 #else
40 NULL
41 #endif
42 ;
43
44 /* Variables */
45 static char *progname;
46 int opt_no_sessiond;
47 char *opt_sessiond_path;
48
49 char *opt_relayd_path;
50
51 enum {
52 OPT_RELAYD_PATH,
53 OPT_SESSION_PATH,
54 OPT_DUMP_OPTIONS,
55 OPT_DUMP_COMMANDS,
56 };
57
58 /* Getopt options. No first level command. */
59 static struct option long_options[] = {
60 {"version", 0, NULL, 'V'},
61 {"help", 0, NULL, 'h'},
62 {"group", 1, NULL, 'g'},
63 {"verbose", 0, NULL, 'v'},
64 {"quiet", 0, NULL, 'q'},
65 {"mi", 1, NULL, 'm'},
66 {"no-sessiond", 0, NULL, 'n'},
67 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
68 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
69 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
70 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
71 {NULL, 0, NULL, 0}
72 };
73
74 /* First level command */
75 static struct cmd_struct commands[] = {
76 { "add-context", cmd_add_context},
77 { "create", cmd_create},
78 { "destroy", cmd_destroy},
79 { "disable-channel", cmd_disable_channels},
80 { "disable-event", cmd_disable_events},
81 { "enable-channel", cmd_enable_channels},
82 { "enable-event", cmd_enable_events},
83 { "help", NULL},
84 { "list", cmd_list},
85 { "load", cmd_load},
86 { "metadata", cmd_metadata},
87 { "regenerate", cmd_regenerate},
88 { "save", cmd_save},
89 { "set-session", cmd_set_session},
90 { "snapshot", cmd_snapshot},
91 { "start", cmd_start},
92 { "status", cmd_status},
93 { "stop", cmd_stop},
94 { "track", cmd_track},
95 { "untrack", cmd_untrack},
96 { "version", cmd_version},
97 { "view", cmd_view},
98 { NULL, NULL} /* Array closure */
99 };
100
101 static void version(FILE *ofp)
102 {
103 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
104 progname,
105 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
106 }
107
108 /*
109 * Find the MI output type enum from a string. This function is for the support
110 * of machine interface output.
111 */
112 static int mi_output_type(const char *output_type)
113 {
114 int ret = 0;
115
116 if (!strncasecmp("xml", output_type, 3)) {
117 ret = LTTNG_MI_XML;
118 } else {
119 /* Invalid output format */
120 ERR("MI output format not supported");
121 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
122 }
123
124 return ret;
125 }
126
127 /*
128 * list_options
129 *
130 * List options line by line. This is mostly for bash auto completion and to
131 * avoid difficult parsing.
132 */
133 static void list_options(FILE *ofp)
134 {
135 int i = 0;
136 struct option *option = NULL;
137
138 option = &long_options[i];
139 while (option->name != NULL) {
140 fprintf(ofp, "--%s\n", option->name);
141
142 if (isprint(option->val)) {
143 fprintf(ofp, "-%c\n", option->val);
144 }
145
146 i++;
147 option = &long_options[i];
148 }
149 }
150
151 /*
152 * clean_exit
153 */
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 void show_basic_help(void)
253 {
254 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
255 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
256 puts("");
257 puts("Available commands:");
258 puts("");
259 puts("Tracing sessions:");
260 puts(" create " CONFIG_CMD_DESCR_CREATE);
261 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
262 puts(" load " CONFIG_CMD_DESCR_LOAD);
263 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE);
264 puts(" save " CONFIG_CMD_DESCR_SAVE);
265 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
266 puts("");
267 puts("Channels:");
268 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
269 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
270 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
271 puts("");
272 puts("Event rules:");
273 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
274 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
275 puts("");
276 puts("Status:");
277 puts(" list " CONFIG_CMD_DESCR_LIST);
278 puts(" status " CONFIG_CMD_DESCR_STATUS);
279 puts("");
280 puts("Control:");
281 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
282 puts(" start " CONFIG_CMD_DESCR_START);
283 puts(" stop " CONFIG_CMD_DESCR_STOP);
284 puts("");
285 puts("Resource tracking:");
286 puts(" track " CONFIG_CMD_DESCR_TRACK);
287 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
288 puts("");
289 puts("Miscellaneous:");
290 puts(" help " CONFIG_CMD_DESCR_HELP);
291 puts(" version " CONFIG_CMD_DESCR_VERSION);
292 puts(" view " CONFIG_CMD_DESCR_VIEW);
293 puts("");
294 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
295 puts("command COMMAND.");
296 puts("");
297 puts("See `man lttng` for more help with the lttng command.");
298 }
299
300 /*
301 * Parse command line arguments.
302 *
303 * Return 0 if OK, else -1
304 */
305 static int parse_args(int argc, char **argv)
306 {
307 int opt, ret;
308
309 if (lttng_is_setuid_setgid()) {
310 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
311 clean_exit(EXIT_FAILURE);
312 }
313
314 if (argc < 2) {
315 show_basic_help();
316 clean_exit(EXIT_FAILURE);
317 }
318
319 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
320 switch (opt) {
321 case 'V':
322 version(stdout);
323 ret = 0;
324 goto end;
325 case 'h':
326 ret = utils_show_help(1, "lttng", help_msg);
327 if (ret) {
328 ERR("Cannot show --help for `lttng`");
329 perror("exec");
330 }
331 goto end;
332 case 'v':
333 /* There is only 3 possible level of verbosity. (-vvv) */
334 if (lttng_opt_verbose < 3) {
335 lttng_opt_verbose += 1;
336 }
337 break;
338 case 'q':
339 lttng_opt_quiet = 1;
340 break;
341 case 'm':
342 lttng_opt_mi = mi_output_type(optarg);
343 if (lttng_opt_mi < 0) {
344 ret = lttng_opt_mi;
345 goto error;
346 }
347 break;
348 case 'g':
349 lttng_set_tracing_group(optarg);
350 break;
351 case 'n':
352 opt_no_sessiond = 1;
353 break;
354 case OPT_SESSION_PATH:
355 free(opt_sessiond_path);
356 opt_sessiond_path = strdup(optarg);
357 if (!opt_sessiond_path) {
358 ret = -1;
359 goto error;
360 }
361 break;
362 case OPT_RELAYD_PATH:
363 free(opt_relayd_path);
364 opt_relayd_path = strdup(optarg);
365 if (!opt_relayd_path) {
366 ret = -1;
367 goto error;
368 }
369 break;
370 case OPT_DUMP_OPTIONS:
371 list_options(stdout);
372 ret = 0;
373 goto end;
374 case OPT_DUMP_COMMANDS:
375 list_commands(commands, stdout);
376 ret = 0;
377 goto end;
378 default:
379 ret = 1;
380 goto error;
381 }
382 }
383
384 /* If both options are specified, quiet wins */
385 if (lttng_opt_verbose && lttng_opt_quiet) {
386 lttng_opt_verbose = 0;
387 }
388
389 /* No leftovers, quit */
390 if ((argc - optind) == 0) {
391 ret = 1;
392 goto error;
393 }
394
395 /*
396 * Handle leftovers which is a first level command with the trailing
397 * options.
398 */
399 ret = handle_command(argc - optind, argv + optind);
400 switch (ret) {
401 case CMD_WARNING:
402 WARN("Some command(s) went wrong");
403 break;
404 case CMD_ERROR:
405 ERR("Command error");
406 break;
407 case CMD_UNDEFINED:
408 ERR("Undefined command or invalid arguments");
409 break;
410 case CMD_FATAL:
411 ERR("Fatal error");
412 break;
413 case CMD_UNSUPPORTED:
414 ERR("Unsupported command");
415 break;
416 case -1:
417 ret = 1;
418 break;
419 case 0:
420 break;
421 default:
422 if (ret < 0) {
423 ret = -ret;
424 }
425 break;
426 }
427
428 end:
429 error:
430 return ret;
431 }
432
433
434 /*
435 * main
436 */
437 int main(int argc, char *argv[])
438 {
439 int ret;
440
441 progname = argv[0] ? argv[0] : "lttng";
442
443 ret = set_signal_handler();
444 if (ret < 0) {
445 clean_exit(ret);
446 }
447
448 ret = parse_args(argc, argv);
449 if (ret != 0) {
450 clean_exit(ret);
451 }
452
453 return 0;
454 }
This page took 0.040757 seconds and 4 git commands to generate.