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