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