Add the lttng view command
[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 as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28 #include <config.h>
29
30 #include <lttng/lttng.h>
31 #include <common/error.h>
32
33 #include "command.h"
34
35 /* Variables */
36 static char *progname;
37
38 int opt_quiet;
39 int opt_verbose;
40 static int opt_no_sessiond;
41 static char *opt_sessiond_path;
42 static pid_t sessiond_pid;
43 static volatile int recv_child_signal;
44
45 enum {
46 OPT_SESSION_PATH,
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
49 };
50
51 /* Getopt options. No first level command. */
52 static struct option long_options[] = {
53 {"help", 0, NULL, 'h'},
54 {"group", 1, NULL, 'g'},
55 {"verbose", 0, NULL, 'v'},
56 {"quiet", 0, NULL, 'q'},
57 {"no-sessiond", 0, NULL, 'n'},
58 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
59 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
60 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
61 {NULL, 0, NULL, 0}
62 };
63
64 /* First level command */
65 static struct cmd_struct commands[] = {
66 { "list", cmd_list},
67 { "create", cmd_create},
68 { "destroy", cmd_destroy},
69 { "start", cmd_start},
70 { "stop", cmd_stop},
71 { "enable-event", cmd_enable_events},
72 { "disable-event", cmd_disable_events},
73 { "enable-channel", cmd_enable_channels},
74 { "disable-channel", cmd_disable_channels},
75 { "add-context", cmd_add_context},
76 { "set-session", cmd_set_session},
77 { "version", cmd_version},
78 { "calibrate", cmd_calibrate},
79 { "view", cmd_view},
80 { NULL, NULL} /* Array closure */
81 };
82
83 static void usage(FILE *ofp)
84 {
85 fprintf(ofp, "LTTng Trace Control " VERSION"\n\n");
86 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND>\n");
87 fprintf(ofp, "\n");
88 fprintf(ofp, "Options:\n");
89 fprintf(ofp, " -h, --help Show this help\n");
90 fprintf(ofp, " --list-options Simple listing of lttng options\n");
91 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
92 fprintf(ofp, " -v, --verbose Increase verbosity\n");
93 fprintf(ofp, " -q, --quiet Quiet mode\n");
94 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
95 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
96 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
97 fprintf(ofp, "\n");
98 fprintf(ofp, "Commands:\n");
99 fprintf(ofp, " add-context Add context to event and/or channel\n");
100 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
101 fprintf(ofp, " create Create tracing session\n");
102 fprintf(ofp, " destroy Tear down tracing session\n");
103 fprintf(ofp, " enable-channel Enable tracing channel\n");
104 fprintf(ofp, " enable-event Enable tracing event\n");
105 fprintf(ofp, " disable-channel Disable tracing channel\n");
106 fprintf(ofp, " disable-event Disable tracing event\n");
107 fprintf(ofp, " list List possible tracing options\n");
108 fprintf(ofp, " set-session Set current session name\n");
109 fprintf(ofp, " start Start tracing\n");
110 fprintf(ofp, " stop Stop tracing\n");
111 fprintf(ofp, " version Show version information\n");
112 fprintf(ofp, " view Start trace viewer\n");
113 fprintf(ofp, "\n");
114 fprintf(ofp, "Each command also has its own -h, --help option.\n");
115 fprintf(ofp, "\n");
116 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
117 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
118 }
119
120 /*
121 * list_options
122 *
123 * List options line by line. This is mostly for bash auto completion and to
124 * avoid difficult parsing.
125 */
126 static void list_options(FILE *ofp)
127 {
128 int i = 0;
129 struct option *option = NULL;
130
131 option = &long_options[i];
132 while (option->name != NULL) {
133 fprintf(ofp, "--%s\n", option->name);
134
135 if (isprint(option->val)) {
136 fprintf(ofp, "-%c\n", option->val);
137 }
138
139 i++;
140 option = &long_options[i];
141 }
142 }
143
144 /*
145 * list_commands
146 *
147 * List commands line by line. This is mostly for bash auto completion and to
148 * avoid difficult parsing.
149 */
150 static void list_commands(FILE *ofp)
151 {
152 int i = 0;
153 struct cmd_struct *cmd = NULL;
154
155 cmd = &commands[i];
156 while (cmd->name != NULL) {
157 fprintf(ofp, "%s\n", cmd->name);
158 i++;
159 cmd = &commands[i];
160 }
161 }
162
163 /*
164 * clean_exit
165 */
166 static void clean_exit(int code)
167 {
168 DBG("Clean exit");
169 exit(code);
170 }
171
172 /*
173 * sighandler
174 *
175 * Signal handler for the daemon
176 */
177 static void sighandler(int sig)
178 {
179 int status;
180
181 switch (sig) {
182 case SIGTERM:
183 DBG("SIGTERM caught");
184 clean_exit(EXIT_FAILURE);
185 break;
186 case SIGCHLD:
187 DBG("SIGCHLD caught");
188 waitpid(sessiond_pid, &status, 0);
189 recv_child_signal = 1;
190 /* Indicate that the session daemon died */
191 sessiond_pid = 0;
192 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
193 break;
194 case SIGUSR1:
195 /* Notify is done */
196 recv_child_signal = 1;
197 DBG("SIGUSR1 caught");
198 break;
199 default:
200 DBG("Unknown signal %d caught", sig);
201 break;
202 }
203
204 return;
205 }
206
207 /*
208 * set_signal_handler
209 *
210 * Setup signal handler for SIGCHLD and SIGTERM.
211 */
212 static int set_signal_handler(void)
213 {
214 int ret = 0;
215 struct sigaction sa;
216 sigset_t sigset;
217
218 if ((ret = sigemptyset(&sigset)) < 0) {
219 perror("sigemptyset");
220 goto end;
221 }
222
223 sa.sa_handler = sighandler;
224 sa.sa_mask = sigset;
225 sa.sa_flags = 0;
226 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
227 perror("sigaction");
228 goto end;
229 }
230
231 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
232 perror("sigaction");
233 goto end;
234 }
235
236 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
237 perror("sigaction");
238 goto end;
239 }
240
241 end:
242 return ret;
243 }
244
245 /*
246 * handle_command
247 *
248 * Handle the full argv list of a first level command. Will find the command
249 * in the global commands array and call the function callback associated.
250 *
251 * If command not found, return -1
252 * else, return function command error code.
253 */
254 static int handle_command(int argc, char **argv)
255 {
256 int i = 0, ret;
257 struct cmd_struct *cmd;
258
259 if (*argv == NULL) {
260 ret = CMD_SUCCESS;
261 goto end;
262 }
263
264 cmd = &commands[i];
265 while (cmd->func != NULL) {
266 /* Find command */
267 if (strcmp(argv[0], cmd->name) == 0) {
268 ret = cmd->func(argc, (const char**) argv);
269 goto end;
270 }
271 i++;
272 cmd = &commands[i];
273 }
274
275 /* Command not found */
276 ret = -1;
277
278 end:
279 return ret;
280 }
281
282 /*
283 * spawn_sessiond
284 *
285 * Spawn a session daemon by forking and execv.
286 */
287 static int spawn_sessiond(char *pathname)
288 {
289 int ret = 0;
290 pid_t pid;
291
292 MSG("Spawning a session daemon");
293 recv_child_signal = 0;
294 pid = fork();
295 if (pid == 0) {
296 /*
297 * Spawn session daemon and tell
298 * it to signal us when ready.
299 */
300 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
301 /* execlp only returns if error happened */
302 if (errno == ENOENT) {
303 ERR("No session daemon found. Use --sessiond-path.");
304 } else {
305 perror("execlp");
306 }
307 kill(getppid(), SIGTERM); /* wake parent */
308 exit(EXIT_FAILURE);
309 } else if (pid > 0) {
310 sessiond_pid = pid;
311 /*
312 * Wait for lttng-sessiond to start. We need to use a flag to check if
313 * the signal has been sent to us, because the child can be scheduled
314 * before the parent, and thus send the signal before this check. In
315 * the signal handler, we set the recv_child_signal flag, so anytime we
316 * check it after the fork is fine. Note that sleep() is interrupted
317 * before the 1 second delay as soon as the signal is received, so it
318 * will not cause visible delay for the user.
319 */
320 while (!recv_child_signal) {
321 sleep(1);
322 }
323 /*
324 * The signal handler will nullify sessiond_pid on SIGCHLD
325 */
326 if (!sessiond_pid) {
327 exit(EXIT_FAILURE);
328 }
329 goto end;
330 } else {
331 perror("fork");
332 ret = -1;
333 goto end;
334 }
335
336 end:
337 return ret;
338 }
339
340 /*
341 * check_sessiond
342 *
343 * Check if the session daemon is available using
344 * the liblttngctl API for the check. If not, try to
345 * spawn a daemon.
346 */
347 static int check_sessiond(void)
348 {
349 int ret;
350 char *pathname = NULL, *alloc_pathname = NULL;
351
352 ret = lttng_session_daemon_alive();
353 if (ret == 0) { /* not alive */
354 /* Try command line option path */
355 if (opt_sessiond_path != NULL) {
356 ret = access(opt_sessiond_path, F_OK | X_OK);
357 if (ret < 0) {
358 ERR("No such file or access denied: %s", opt_sessiond_path);
359 goto end;
360 }
361 pathname = opt_sessiond_path;
362 } else {
363 /* Try LTTNG_SESSIOND_PATH env variable */
364 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
365 }
366
367 /* Let's rock and roll */
368 if (pathname == NULL) {
369 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
370 if (ret < 0) {
371 perror("asprintf spawn sessiond");
372 goto end;
373 }
374 pathname = alloc_pathname;
375 }
376
377 ret = spawn_sessiond(pathname);
378 free(alloc_pathname);
379 if (ret < 0) {
380 ERR("Problem occurred when starting %s", pathname);
381 goto end;
382 }
383 }
384
385 end:
386 printf("HIT essiond %d\n", ret);
387 return ret;
388 }
389
390 /*
391 * Check args for specific options that *must* not trigger a session daemon
392 * execution.
393 *
394 * Return 1 if match else 0.
395 */
396 static int check_args_no_sessiond(int argc, char **argv)
397 {
398 int i;
399
400 for (i = 0; i < argc; i++) {
401 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
402 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
403 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
404 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
405 strncmp(argv[i], "version", sizeof("version")) == 0 ||
406 strncmp(argv[i], "view", sizeof("view")) == 0) {
407 return 1;
408 }
409 }
410
411 return 0;
412 }
413
414 /*
415 * Parse command line arguments.
416 *
417 * Return 0 if OK, else -1
418 */
419 static int parse_args(int argc, char **argv)
420 {
421 int opt, ret;
422
423 if (argc < 2) {
424 usage(stderr);
425 clean_exit(EXIT_FAILURE);
426 }
427
428 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
429 switch (opt) {
430 case 'h':
431 usage(stdout);
432 ret = 0;
433 goto end;
434 case 'v':
435 opt_verbose += 1;
436 break;
437 case 'q':
438 opt_quiet = 1;
439 break;
440 case 'g':
441 lttng_set_tracing_group(optarg);
442 break;
443 case 'n':
444 opt_no_sessiond = 1;
445 break;
446 case OPT_SESSION_PATH:
447 opt_sessiond_path = strdup(optarg);
448 break;
449 case OPT_DUMP_OPTIONS:
450 list_options(stdout);
451 ret = 0;
452 goto end;
453 case OPT_DUMP_COMMANDS:
454 list_commands(stdout);
455 ret = 0;
456 goto end;
457 default:
458 usage(stderr);
459 ret = 1;
460 goto error;
461 }
462 }
463
464 /* If both options are specified, quiet wins */
465 if (opt_verbose && opt_quiet) {
466 opt_verbose = 0;
467 }
468
469 /* Spawn session daemon if needed */
470 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
471 (check_sessiond() < 0)) {
472 ret = 1;
473 goto error;
474 }
475
476 /* No leftovers, print usage and quit */
477 if ((argc - optind) == 0) {
478 usage(stderr);
479 ret = 1;
480 goto error;
481 }
482
483 /*
484 * Handle leftovers which is a first level command with the trailing
485 * options.
486 */
487 ret = handle_command(argc - optind, argv + optind);
488 switch (ret) {
489 case CMD_WARNING:
490 WARN("Some command(s) went wrong");
491 break;
492 case CMD_ERROR:
493 ERR("Command error");
494 break;
495 case CMD_UNDEFINED:
496 ERR("Undefined command");
497 break;
498 case CMD_FATAL:
499 ERR("Fatal error");
500 break;
501 case -1:
502 usage(stderr);
503 ret = 1;
504 break;
505 case 0:
506 break;
507 default:
508 ERR("%s", lttng_strerror(ret));
509 break;
510 }
511
512 end:
513 error:
514 return ret;
515 }
516
517
518 /*
519 * main
520 */
521 int main(int argc, char *argv[])
522 {
523 int ret;
524
525 progname = argv[0] ? argv[0] : "lttng";
526
527 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
528 if (strncmp(progname, "drtrace", 7) == 0 ||
529 strncmp("compudj", getenv("USER"), 7) == 0) {
530 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
531 }
532
533 ret = set_signal_handler();
534 if (ret < 0) {
535 clean_exit(ret);
536 }
537
538 ret = parse_args(argc, argv);
539 if (ret != 0) {
540 clean_exit(ret);
541 }
542
543 return 0;
544 }
This page took 0.040486 seconds and 5 git commands to generate.