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