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