Fix UST tests if lttng-ust is not available
[lttng-tools.git] / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
fac6795d
DG
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.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
fac6795d 20#include <getopt.h>
f3ed775e 21#include <signal.h>
fac6795d
DG
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
fac6795d 25#include <unistd.h>
3bd1e081 26#include <config.h>
fac6795d 27
5b97ec60 28#include <lttng/lttng.h>
fac6795d 29
f3ed775e 30#include "cmd.h"
beb8c75a 31#include "conf.h"
fac6795d
DG
32#include "lttngerr.h"
33
34/* Variables */
35static char *progname;
fac6795d 36
f3ed775e
DG
37int opt_quiet;
38int opt_verbose;
39static int opt_no_sessiond;
40static char *opt_sessiond_path;
41
42enum {
f3ed775e 43 OPT_SESSION_PATH,
865abf65
SM
44 OPT_DUMP_OPTIONS,
45 OPT_DUMP_COMMANDS,
f3ed775e
DG
46};
47
48/* Getopt options. No first level command. */
49static struct option long_options[] = {
50 {"help", 0, NULL, 'h'},
51 {"group", 1, NULL, 'g'},
52 {"verbose", 0, NULL, 'v'},
53 {"quiet", 0, NULL, 'q'},
8490ae7b 54 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 55 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
865abf65
SM
56 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
57 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
58 {NULL, 0, NULL, 0}
59};
60
61/* First level command */
62static struct cmd_struct commands[] = {
63 { "list", cmd_list},
64 { "create", cmd_create},
65 { "destroy", cmd_destroy},
f3ed775e
DG
66 { "start", cmd_start},
67 { "stop", cmd_stop},
68 { "enable-event", cmd_enable_events},
e953ef25 69 { "disable-event", cmd_disable_events},
d36b8583 70 { "enable-channel", cmd_enable_channels},
26cc6b4e 71 { "disable-channel", cmd_disable_channels},
d65106b1 72 { "add-context", cmd_add_context},
3087ef18 73 { "set-session", cmd_set_session},
eb9cb8b7 74 { "version", cmd_version},
d0254c7c 75 { "calibrate", cmd_calibrate},
f3ed775e
DG
76 { NULL, NULL} /* Array closure */
77};
78
79static void usage(FILE *ofp)
8c0faa1d 80{
f3ed775e
DG
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");
8490ae7b 89 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
f3ed775e 90 fprintf(ofp, " --sessiond-path Session daemon full path\n");
865abf65
SM
91 fprintf(ofp, " --list-options Simple listing of lttng options\n");
92 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
f3ed775e
DG
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Commands:\n");
d65106b1 95 fprintf(ofp, " add-context Add context to event or/and channel\n");
d0254c7c 96 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
f3ed775e
DG
97 fprintf(ofp, " create Create tracing session\n");
98 fprintf(ofp, " destroy Teardown tracing session\n");
d36b8583 99 fprintf(ofp, " enable-channel Enable tracing channel\n");
26cc6b4e
DG
100 fprintf(ofp, " enable-event Enable tracing event\n");
101 fprintf(ofp, " disable-channel Disable tracing channel\n");
f3ed775e
DG
102 fprintf(ofp, " disable-event Disable tracing event\n");
103 fprintf(ofp, " list List possible tracing options\n");
3087ef18 104 fprintf(ofp, " set-session Set current session name\n");
f3ed775e
DG
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");
8c0faa1d
DG
111}
112
865abf65
SM
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 */
119static 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 */
143static 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
7442b2ba 156/*
f3ed775e 157 * clean_exit
96243366 158 */
f3ed775e 159static void clean_exit(int code)
96243366 160{
f3ed775e
DG
161 DBG("Clean exit");
162 exit(code);
894be886 163}
96243366 164
894be886 165/*
f3ed775e 166 * sighandler
2ef84c95 167 *
f3ed775e 168 * Signal handler for the daemon
2ef84c95 169 */
f3ed775e 170static void sighandler(int sig)
2ef84c95 171{
f3ed775e
DG
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;
2ef84c95
DG
184 }
185
f3ed775e 186 return;
2ef84c95
DG
187}
188
189/*
f3ed775e 190 * set_signal_handler
894be886 191 *
f3ed775e 192 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 193 */
f3ed775e 194static int set_signal_handler(void)
894be886 195{
f3ed775e
DG
196 int ret = 0;
197 struct sigaction sa;
198 sigset_t sigset;
33a2b854 199
f3ed775e
DG
200 if ((ret = sigemptyset(&sigset)) < 0) {
201 perror("sigemptyset");
33a2b854
DG
202 goto end;
203 }
204
f3ed775e
DG
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");
96243366
DG
210 goto end;
211 }
212
f3ed775e
DG
213 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
214 perror("sigaction");
215 goto end;
894be886
DG
216 }
217
96243366 218end:
57167058
DG
219 return ret;
220}
221
fac6795d 222/*
f3ed775e 223 * handle_command
fac6795d 224 *
f3ed775e
DG
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.
1c9f7941 227 *
f3ed775e
DG
228 * If command not found, return -1
229 * else, return function command error code.
1c9f7941 230 */
f3ed775e 231static int handle_command(int argc, char **argv)
1c9f7941 232{
f3ed775e
DG
233 int i = 0, ret;
234 struct cmd_struct *cmd;
1c9f7941 235
f3ed775e
DG
236 if (*argv == NULL) {
237 ret = CMD_SUCCESS;
47b74d63 238 goto end;
1c9f7941
DG
239 }
240
f3ed775e
DG
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;
894be886 261 }
f3ed775e
DG
262 i++;
263 cmd = &commands[i];
894be886
DG
264 }
265
f3ed775e
DG
266 /* Command not found */
267 ret = -1;
894be886
DG
268
269end:
f3ed775e 270 return ret;
8548ff30
DG
271}
272
5b8719f5
DG
273/*
274 * spawn_sessiond
275 *
276 * Spawn a session daemon by forking and execv.
277 */
278static int spawn_sessiond(char *pathname)
279{
280 int ret = 0;
281 pid_t pid;
282
f3ed775e 283 MSG("Spawning a session daemon");
5b8719f5
DG
284 pid = fork();
285 if (pid == 0) {
5e16da05
MD
286 /*
287 * Spawn session daemon and tell
5b8719f5
DG
288 * it to signal us when ready.
289 */
32258573 290 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
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");
5b8719f5 296 }
5e16da05
MD
297 kill(getppid(), SIGTERM); /* unpause parent */
298 exit(EXIT_FAILURE);
5b8719f5 299 } else if (pid > 0) {
32258573 300 /* Wait for lttng-sessiond to start */
5b8719f5
DG
301 pause();
302 goto end;
303 } else {
304 perror("fork");
305 ret = -1;
306 goto end;
307 }
308
309end:
310 return ret;
311}
312
fac6795d 313/*
f3ed775e 314 * check_sessiond
fac6795d
DG
315 *
316 * Check if the session daemon is available using
5b8719f5
DG
317 * the liblttngctl API for the check. If not, try to
318 * spawn a daemon.
fac6795d 319 */
f3ed775e 320static int check_sessiond(void)
fac6795d
DG
321{
322 int ret;
5e16da05 323 char *pathname = NULL, *alloc_pathname = NULL;
fac6795d 324
947308c4
DG
325 ret = lttng_session_daemon_alive();
326 if (ret == 0) { /* not alive */
5b8719f5
DG
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 */
e8f07c63 337 pathname = getenv(LTTNG_SESSIOND_PATH_ENV);
5b8719f5
DG
338 }
339
340 /* Let's rock and roll */
341 if (pathname == NULL) {
32258573 342 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
5b8719f5 343 if (ret < 0) {
3f5fa9ed 344 perror("asprintf spawn sessiond");
5b8719f5
DG
345 goto end;
346 }
5e16da05 347 pathname = alloc_pathname;
5b8719f5
DG
348 }
349
350 ret = spawn_sessiond(pathname);
5e16da05 351 free(alloc_pathname);
5b8719f5
DG
352 if (ret < 0) {
353 ERR("Problem occurs when starting %s", pathname);
354 goto end;
355 }
fac6795d
DG
356 }
357
5b8719f5 358end:
fac6795d
DG
359 return ret;
360}
361
5b8719f5 362/*
f3ed775e 363 * parse_args
5b8719f5 364 *
f3ed775e
DG
365 * Parse command line arguments.
366 * Return 0 if OK, else -1
5b8719f5 367 */
f3ed775e 368static int parse_args(int argc, char **argv)
5b8719f5 369{
f3ed775e 370 int opt, ret;
5b8719f5 371
f3ed775e
DG
372 if (argc < 2) {
373 usage(stderr);
374 clean_exit(EXIT_FAILURE);
5b8719f5
DG
375 }
376
8490ae7b 377 while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) {
f3ed775e
DG
378 switch (opt) {
379 case 'h':
380 usage(stderr);
381 goto error;
382 case 'v':
b551a063 383 opt_verbose += 1;
f3ed775e
DG
384 break;
385 case 'q':
386 opt_quiet = 1;
387 break;
388 case 'g':
389 lttng_set_tracing_group(optarg);
390 break;
8490ae7b 391 case 'n':
f3ed775e
DG
392 opt_no_sessiond = 1;
393 break;
394 case OPT_SESSION_PATH:
395 opt_sessiond_path = strdup(optarg);
396 break;
865abf65
SM
397 case OPT_DUMP_OPTIONS:
398 list_options(stdout);
399 ret = 0;
400 goto error;
401 case OPT_DUMP_COMMANDS:
402 list_commands(stdout);
403 ret = 0;
404 goto error;
f3ed775e
DG
405 default:
406 usage(stderr);
407 goto error;
408 }
5b8719f5 409 }
fac6795d 410
f3ed775e
DG
411 /* If both options are specified, quiet wins */
412 if (opt_verbose && opt_quiet) {
413 opt_verbose = 0;
5b8719f5
DG
414 }
415
f3ed775e
DG
416 /* Spawn session daemon if needed */
417 if (opt_no_sessiond == 0 && (check_sessiond() < 0)) {
418 goto error;
5b8719f5
DG
419 }
420
f3ed775e
DG
421 /* No leftovers, print usage and quit */
422 if ((argc - optind) == 0) {
423 usage(stderr);
424 goto error;
425 }
7442b2ba 426
f3ed775e
DG
427 /*
428 * Handle leftovers which is a first level command with the trailing
429 * options.
430 */
431 ret = handle_command(argc - optind, argv + optind);
432 if (ret < 0) {
433 if (ret == -1) {
434 usage(stderr);
f3ed775e 435 } else {
9a745bc7 436 ERR("%s", lttng_strerror(ret));
f3ed775e 437 }
1fff1faa 438 goto error;
96243366
DG
439 }
440
1fff1faa 441 return 0;
f3ed775e
DG
442
443error:
444 return -1;
fac6795d
DG
445}
446
f3ed775e 447
fac6795d 448/*
5b8719f5 449 * main
fac6795d
DG
450 */
451int main(int argc, char *argv[])
452{
453 int ret;
454
455 progname = argv[0] ? argv[0] : "lttng";
456
457 /* For Mathieu Desnoyers aka Dr Tracing */
458 if (strncmp(progname, "drtrace", 7) == 0) {
459 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
460 }
461
5b8719f5
DG
462 ret = set_signal_handler();
463 if (ret < 0) {
87378cf5 464 clean_exit(ret);
5b8719f5
DG
465 }
466
f3ed775e 467 ret = parse_args(argc, argv);
1fff1faa
DG
468 if (ret < 0) {
469 clean_exit(EXIT_FAILURE);
470 }
87378cf5 471
fac6795d
DG
472 return 0;
473}
This page took 0.052461 seconds and 5 git commands to generate.