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