Add error message if event not found
[lttng-tools.git] / 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 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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 <unistd.h>
26
27 #include <lttng/lttng.h>
28
29 #include "cmd.h"
30 #include "conf.h"
31 #include "lttngerr.h"
32
33 /* Variables */
34 static char *progname;
35
36 int opt_quiet;
37 int opt_verbose;
38 static int opt_no_sessiond;
39 static char *opt_sessiond_path;
40
41 enum {
42 OPT_NO_SESSIOND,
43 OPT_SESSION_PATH,
44 OPT_DUMP_OPTIONS,
45 OPT_DUMP_COMMANDS,
46 };
47
48 /* Getopt options. No first level command. */
49 static struct option long_options[] = {
50 {"help", 0, NULL, 'h'},
51 {"group", 1, NULL, 'g'},
52 {"verbose", 0, NULL, 'v'},
53 {"quiet", 0, NULL, 'q'},
54 {"no-sessiond", 0, NULL, OPT_NO_SESSIOND},
55 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
56 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
57 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
58 {NULL, 0, NULL, 0}
59 };
60
61 /* First level command */
62 static struct cmd_struct commands[] = {
63 { "list", cmd_list},
64 { "create", cmd_create},
65 { "destroy", cmd_destroy},
66 { "add-channel", cmd_add_channel},
67 { "start", cmd_start},
68 { "stop", cmd_stop},
69 { "enable-event", cmd_enable_events},
70 { "disable-event", cmd_disable_events},
71 { "enable-channel", cmd_enable_channels},
72 { "disable-channel", cmd_disable_channels},
73 { "add-context", cmd_add_context},
74 { "set-session", cmd_set_session},
75 { "version", cmd_version},
76 { NULL, NULL} /* Array closure */
77 };
78
79 static void usage(FILE *ofp)
80 {
81 fprintf(ofp, "LTTng Trace Control " VERSION"\n\n");
82 fprintf(ofp, "usage: lttng [options] <command>\n");
83 fprintf(ofp, "\n");
84 fprintf(ofp, "Options:\n");
85 fprintf(ofp, " -h, --help Show this help\n");
86 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
87 fprintf(ofp, " -v, --verbose Verbose mode\n");
88 fprintf(ofp, " -q, --quiet Quiet mode\n");
89 fprintf(ofp, " --no-sessiond Don't spawn a session daemon\n");
90 fprintf(ofp, " --sessiond-path Session daemon full path\n");
91 fprintf(ofp, " --list-options Simple listing of lttng options\n");
92 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Commands:\n");
95 fprintf(ofp, " add-channel Add channel to tracer\n");
96 fprintf(ofp, " add-context Add context to event or/and channel\n");
97 fprintf(ofp, " create Create tracing session\n");
98 fprintf(ofp, " destroy Teardown tracing session\n");
99 fprintf(ofp, " enable-channel Enable tracing channel\n");
100 fprintf(ofp, " enable-event Enable tracing event\n");
101 fprintf(ofp, " disable-channel Disable tracing channel\n");
102 fprintf(ofp, " disable-event Disable tracing event\n");
103 fprintf(ofp, " list List possible tracing options\n");
104 fprintf(ofp, " set-session Set current session name\n");
105 fprintf(ofp, " start Start tracing\n");
106 fprintf(ofp, " stop Stop tracing\n");
107 fprintf(ofp, " version Show version information\n");
108 fprintf(ofp, "\n");
109 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
110 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
111 }
112
113 /*
114 * list_options
115 *
116 * List options line by line. This is mostly for bash auto completion and to
117 * avoid difficult parsing.
118 */
119 static void list_options(FILE *ofp)
120 {
121 int i = 0;
122 struct option *option = NULL;
123
124 option = &long_options[i];
125 while (option->name != NULL) {
126 fprintf(ofp, "--%s\n", option->name);
127
128 if (isprint(option->val)) {
129 fprintf(ofp, "-%c\n", option->val);
130 }
131
132 i++;
133 option = &long_options[i];
134 }
135 }
136
137 /*
138 * list_commands
139 *
140 * List commands line by line. This is mostly for bash auto completion and to
141 * avoid difficult parsing.
142 */
143 static void list_commands(FILE *ofp)
144 {
145 int i = 0;
146 struct cmd_struct *cmd = NULL;
147
148 cmd = &commands[i];
149 while (cmd->name != NULL) {
150 fprintf(ofp, "%s\n", cmd->name);
151 i++;
152 cmd = &commands[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 catched");
175 clean_exit(EXIT_FAILURE);
176 break;
177 case SIGCHLD:
178 /* Notify is done */
179 DBG("SIGCHLD catched");
180 break;
181 default:
182 DBG("Unknown signal %d catched", sig);
183 break;
184 }
185
186 return;
187 }
188
189 /*
190 * set_signal_handler
191 *
192 * Setup signal handler for SIGCHLD and SIGTERM.
193 */
194 static int set_signal_handler(void)
195 {
196 int ret = 0;
197 struct sigaction sa;
198 sigset_t sigset;
199
200 if ((ret = sigemptyset(&sigset)) < 0) {
201 perror("sigemptyset");
202 goto end;
203 }
204
205 sa.sa_handler = sighandler;
206 sa.sa_mask = sigset;
207 sa.sa_flags = 0;
208 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
209 perror("sigaction");
210 goto end;
211 }
212
213 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
214 perror("sigaction");
215 goto end;
216 }
217
218 end:
219 return ret;
220 }
221
222 /*
223 * handle_command
224 *
225 * Handle the full argv list of a first level command. Will find the command
226 * in the global commands array and call the function callback associated.
227 *
228 * If command not found, return -1
229 * else, return function command error code.
230 */
231 static int handle_command(int argc, char **argv)
232 {
233 int i = 0, ret;
234 struct cmd_struct *cmd;
235
236 if (*argv == NULL) {
237 ret = CMD_SUCCESS;
238 goto end;
239 }
240
241 cmd = &commands[i];
242 while (cmd->func != NULL) {
243 /* Find command */
244 if (strcmp(argv[0], cmd->name) == 0) {
245 ret = cmd->func(argc, (const char**) argv);
246 switch (ret) {
247 case CMD_ERROR:
248 ERR("Command error");
249 break;
250 case CMD_NOT_IMPLEMENTED:
251 ERR("Options not implemented");
252 break;
253 case CMD_UNDEFINED:
254 ERR("Undefined command");
255 break;
256 case CMD_FATAL:
257 ERR("Fatal error");
258 break;
259 }
260 goto end;
261 }
262 i++;
263 cmd = &commands[i];
264 }
265
266 /* Command not found */
267 ret = -1;
268
269 end:
270 return ret;
271 }
272
273 /*
274 * spawn_sessiond
275 *
276 * Spawn a session daemon by forking and execv.
277 */
278 static int spawn_sessiond(char *pathname)
279 {
280 int ret = 0;
281 pid_t pid;
282
283 MSG("Spawning a session daemon");
284 pid = fork();
285 if (pid == 0) {
286 /*
287 * Spawn session daemon and tell
288 * it to signal us when ready.
289 */
290 execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
291 /* execlp only returns if error happened */
292 if (errno == ENOENT) {
293 ERR("No session daemon found. Use --sessiond-path.");
294 } else {
295 perror("execlp");
296 }
297 kill(getppid(), SIGTERM); /* unpause parent */
298 exit(EXIT_FAILURE);
299 } else if (pid > 0) {
300 /* Wait for ltt-sessiond to start */
301 pause();
302 goto end;
303 } else {
304 perror("fork");
305 ret = -1;
306 goto end;
307 }
308
309 end:
310 return ret;
311 }
312
313 /*
314 * check_sessiond
315 *
316 * Check if the session daemon is available using
317 * the liblttngctl API for the check. If not, try to
318 * spawn a daemon.
319 */
320 static int check_sessiond(void)
321 {
322 int ret;
323 char *pathname = NULL, *alloc_pathname = NULL;
324
325 ret = lttng_session_daemon_alive();
326 if (ret == 0) { /* not alive */
327 /* Try command line option path */
328 if (opt_sessiond_path != NULL) {
329 ret = access(opt_sessiond_path, F_OK | X_OK);
330 if (ret < 0) {
331 ERR("No such file: %s", opt_sessiond_path);
332 goto end;
333 }
334 pathname = opt_sessiond_path;
335 } else {
336 /* Try LTTNG_SESSIOND_PATH env variable */
337 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
338 }
339
340 /* Let's rock and roll */
341 if (pathname == NULL) {
342 ret = asprintf(&alloc_pathname, "ltt-sessiond");
343 if (ret < 0) {
344 goto end;
345 }
346 pathname = alloc_pathname;
347 }
348
349 ret = spawn_sessiond(pathname);
350 free(alloc_pathname);
351 if (ret < 0) {
352 ERR("Problem occurs when starting %s", pathname);
353 goto end;
354 }
355 }
356
357 end:
358 return ret;
359 }
360
361 /*
362 * parse_args
363 *
364 * Parse command line arguments.
365 * Return 0 if OK, else -1
366 */
367 static int parse_args(int argc, char **argv)
368 {
369 int opt, ret;
370
371 if (argc < 2) {
372 usage(stderr);
373 clean_exit(EXIT_FAILURE);
374 }
375
376 while ((opt = getopt_long(argc, argv, "+hvqg:", long_options, NULL)) != -1) {
377 switch (opt) {
378 case 'h':
379 usage(stderr);
380 goto error;
381 case 'v':
382 opt_verbose = 1;
383 break;
384 case 'q':
385 opt_quiet = 1;
386 break;
387 case 'g':
388 lttng_set_tracing_group(optarg);
389 break;
390 case OPT_NO_SESSIOND:
391 opt_no_sessiond = 1;
392 break;
393 case OPT_SESSION_PATH:
394 opt_sessiond_path = strdup(optarg);
395 break;
396 case OPT_DUMP_OPTIONS:
397 list_options(stdout);
398 ret = 0;
399 goto error;
400 case OPT_DUMP_COMMANDS:
401 list_commands(stdout);
402 ret = 0;
403 goto error;
404 default:
405 usage(stderr);
406 goto error;
407 }
408 }
409
410 /* If both options are specified, quiet wins */
411 if (opt_verbose && opt_quiet) {
412 opt_verbose = 0;
413 }
414
415 /* Spawn session daemon if needed */
416 if (opt_no_sessiond == 0 && (check_sessiond() < 0)) {
417 goto error;
418 }
419
420 /* No leftovers, print usage and quit */
421 if ((argc - optind) == 0) {
422 usage(stderr);
423 goto error;
424 }
425
426 /*
427 * Handle leftovers which is a first level command with the trailing
428 * options.
429 */
430 ret = handle_command(argc - optind, argv + optind);
431 if (ret < 0) {
432 if (ret == -1) {
433 usage(stderr);
434 } else {
435 ERR("%s", lttng_get_readable_code(ret));
436 }
437 goto error;
438 }
439
440 return 0;
441
442 error:
443 return -1;
444 }
445
446
447 /*
448 * main
449 */
450 int main(int argc, char *argv[])
451 {
452 int ret;
453
454 progname = argv[0] ? argv[0] : "lttng";
455
456 /* For Mathieu Desnoyers aka Dr Tracing */
457 if (strncmp(progname, "drtrace", 7) == 0) {
458 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
459 }
460
461 ret = set_signal_handler();
462 if (ret < 0) {
463 clean_exit(ret);
464 }
465
466 ret = parse_args(argc, argv);
467 if (ret < 0) {
468 clean_exit(EXIT_FAILURE);
469 }
470
471 return 0;
472 }
This page took 0.059547 seconds and 4 git commands to generate.