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