4d2df2deb999832f1d816eefc44ebb441e7ce614
[lttng-tools.git] / src / bin / 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, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28 #include <config.h>
29 #include <ctype.h>
30
31 #include <lttng/lttng.h>
32 #include <common/error.h>
33 #include <common/compat/getenv.h>
34
35 #include "command.h"
36
37 /* Variables */
38 static char *progname;
39 int opt_no_sessiond;
40 char *opt_sessiond_path;
41 pid_t sessiond_pid;
42 volatile int recv_child_signal;
43
44 char *opt_relayd_path;
45
46 enum {
47 OPT_RELAYD_PATH,
48 OPT_SESSION_PATH,
49 OPT_DUMP_OPTIONS,
50 OPT_DUMP_COMMANDS,
51 };
52
53 /* Getopt options. No first level command. */
54 static struct option long_options[] = {
55 {"version", 0, NULL, 'V'},
56 {"help", 0, NULL, 'h'},
57 {"group", 1, NULL, 'g'},
58 {"verbose", 0, NULL, 'v'},
59 {"quiet", 0, NULL, 'q'},
60 {"mi", 1, NULL, 'm'},
61 {"no-sessiond", 0, NULL, 'n'},
62 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
63 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
64 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
65 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
66 {NULL, 0, NULL, 0}
67 };
68
69 /* First level command */
70 static struct cmd_struct commands[] = {
71 { "list", cmd_list},
72 { "create", cmd_create},
73 { "destroy", cmd_destroy},
74 { "start", cmd_start},
75 { "stop", cmd_stop},
76 { "enable-event", cmd_enable_events},
77 { "disable-event", cmd_disable_events},
78 { "enable-channel", cmd_enable_channels},
79 { "disable-channel", cmd_disable_channels},
80 { "add-context", cmd_add_context},
81 { "set-session", cmd_set_session},
82 { "version", cmd_version},
83 { "calibrate", cmd_calibrate},
84 { "view", cmd_view},
85 { "snapshot", cmd_snapshot},
86 { "save", cmd_save},
87 { "load", cmd_load},
88 { "track", cmd_track},
89 { "untrack", cmd_untrack},
90 { NULL, NULL} /* Array closure */
91 };
92
93 static void usage(FILE *ofp)
94 {
95 fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n",
96 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
97 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
98 fprintf(ofp, "\n");
99 fprintf(ofp, "Options:\n");
100 fprintf(ofp, " -V, --version Show version\n");
101 fprintf(ofp, " -h, --help Show this help\n");
102 fprintf(ofp, " --list-options Simple listing of lttng options\n");
103 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
104 fprintf(ofp, " -v, --verbose Increase verbosity\n");
105 fprintf(ofp, " -q, --quiet Quiet mode\n");
106 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
107 fprintf(ofp, " Type: xml\n");
108 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
109 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
110 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
111 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
112 fprintf(ofp, "\n");
113 fprintf(ofp, "Commands:\n");
114 fprintf(ofp, " add-context Add context to event and/or channel\n");
115 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
116 fprintf(ofp, " create Create tracing session\n");
117 fprintf(ofp, " destroy Tear down tracing session\n");
118 fprintf(ofp, " enable-channel Enable tracing channel\n");
119 fprintf(ofp, " enable-event Enable tracing event\n");
120 fprintf(ofp, " disable-channel Disable tracing channel\n");
121 fprintf(ofp, " disable-event Disable tracing event\n");
122 fprintf(ofp, " list List possible tracing options\n");
123 fprintf(ofp, " set-session Set current session name\n");
124 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
125 fprintf(ofp, " start Start tracing\n");
126 fprintf(ofp, " stop Stop tracing\n");
127 fprintf(ofp, " version Show version information\n");
128 fprintf(ofp, " view Start trace viewer\n");
129 fprintf(ofp, " save Save session configuration\n");
130 fprintf(ofp, " load Load session configuration\n");
131 fprintf(ofp, " track Track specific system resources\n");
132 fprintf(ofp, " untrack Untrack specific system resources\n");
133 fprintf(ofp, "\n");
134 fprintf(ofp, "Each command also has its own -h, --help option.\n");
135 fprintf(ofp, "\n");
136 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
137 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
138 }
139
140 static void version(FILE *ofp)
141 {
142 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
143 progname,
144 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
145 }
146
147 /*
148 * Find the MI output type enum from a string. This function is for the support
149 * of machine interface output.
150 */
151 static int mi_output_type(const char *output_type)
152 {
153 int ret = 0;
154
155 if (!strncasecmp("xml", output_type, 3)) {
156 ret = LTTNG_MI_XML;
157 } else {
158 /* Invalid output format */
159 ERR("MI output format not supported");
160 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
161 }
162
163 return ret;
164 }
165
166 /*
167 * list_options
168 *
169 * List options line by line. This is mostly for bash auto completion and to
170 * avoid difficult parsing.
171 */
172 static void list_options(FILE *ofp)
173 {
174 int i = 0;
175 struct option *option = NULL;
176
177 option = &long_options[i];
178 while (option->name != NULL) {
179 fprintf(ofp, "--%s\n", option->name);
180
181 if (isprint(option->val)) {
182 fprintf(ofp, "-%c\n", option->val);
183 }
184
185 i++;
186 option = &long_options[i];
187 }
188 }
189
190 /*
191 * clean_exit
192 */
193 static void clean_exit(int code)
194 {
195 DBG("Clean exit");
196 exit(code);
197 }
198
199 /*
200 * sighandler
201 *
202 * Signal handler for the daemon
203 */
204 static void sighandler(int sig)
205 {
206 int status;
207
208 switch (sig) {
209 case SIGTERM:
210 DBG("SIGTERM caught");
211 clean_exit(EXIT_FAILURE);
212 break;
213 case SIGCHLD:
214 DBG("SIGCHLD caught");
215 waitpid(sessiond_pid, &status, 0);
216 recv_child_signal = 1;
217 /* Indicate that the session daemon died */
218 sessiond_pid = 0;
219 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
220 break;
221 case SIGUSR1:
222 /* Notify is done */
223 recv_child_signal = 1;
224 DBG("SIGUSR1 caught");
225 break;
226 default:
227 DBG("Unknown signal %d caught", sig);
228 break;
229 }
230
231 return;
232 }
233
234 /*
235 * set_signal_handler
236 *
237 * Setup signal handler for SIGCHLD and SIGTERM.
238 */
239 static int set_signal_handler(void)
240 {
241 int ret = 0;
242 struct sigaction sa;
243 sigset_t sigset;
244
245 if ((ret = sigemptyset(&sigset)) < 0) {
246 PERROR("sigemptyset");
247 goto end;
248 }
249
250 sa.sa_handler = sighandler;
251 sa.sa_mask = sigset;
252 sa.sa_flags = 0;
253 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
254 PERROR("sigaction");
255 goto end;
256 }
257
258 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
259 PERROR("sigaction");
260 goto end;
261 }
262
263 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
264 PERROR("sigaction");
265 goto end;
266 }
267
268 end:
269 return ret;
270 }
271
272 /*
273 * handle_command
274 *
275 * Handle the full argv list of a first level command. Will find the command
276 * in the global commands array and call the function callback associated.
277 *
278 * If command not found, return -1
279 * else, return function command error code.
280 */
281 static int handle_command(int argc, char **argv)
282 {
283 int i = 0, ret;
284 struct cmd_struct *cmd;
285
286 if (*argv == NULL) {
287 ret = CMD_SUCCESS;
288 goto end;
289 }
290
291 cmd = &commands[i];
292 while (cmd->func != NULL) {
293 /* Find command */
294 if (strcmp(argv[0], cmd->name) == 0) {
295 ret = cmd->func(argc, (const char**) argv);
296 goto end;
297 }
298 i++;
299 cmd = &commands[i];
300 }
301
302 /* Command not found */
303 ret = CMD_UNDEFINED;
304
305 end:
306 return ret;
307 }
308
309 /*
310 * Parse command line arguments.
311 *
312 * Return 0 if OK, else -1
313 */
314 static int parse_args(int argc, char **argv)
315 {
316 int opt, ret;
317 char *user;
318
319 if (lttng_is_setuid_setgid()) {
320 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
321 clean_exit(EXIT_FAILURE);
322 }
323
324 if (argc < 2) {
325 usage(stderr);
326 clean_exit(EXIT_FAILURE);
327 }
328
329 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
330 switch (opt) {
331 case 'V':
332 version(stdout);
333 ret = 0;
334 goto end;
335 case 'h':
336 usage(stdout);
337 ret = 0;
338 goto end;
339 case 'v':
340 /* There is only 3 possible level of verbosity. (-vvv) */
341 if (lttng_opt_verbose < 3) {
342 lttng_opt_verbose += 1;
343 }
344 break;
345 case 'q':
346 lttng_opt_quiet = 1;
347 break;
348 case 'm':
349 lttng_opt_mi = mi_output_type(optarg);
350 if (lttng_opt_mi < 0) {
351 ret = lttng_opt_mi;
352 goto error;
353 }
354 break;
355 case 'g':
356 lttng_set_tracing_group(optarg);
357 break;
358 case 'n':
359 opt_no_sessiond = 1;
360 break;
361 case OPT_SESSION_PATH:
362 opt_sessiond_path = strdup(optarg);
363 if (!opt_sessiond_path) {
364 ret = -1;
365 goto error;
366 }
367 break;
368 case OPT_RELAYD_PATH:
369 opt_relayd_path = strdup(optarg);
370 if (!opt_relayd_path) {
371 ret = -1;
372 goto error;
373 }
374 break;
375 case OPT_DUMP_OPTIONS:
376 list_options(stdout);
377 ret = 0;
378 goto end;
379 case OPT_DUMP_COMMANDS:
380 list_commands(commands, stdout);
381 ret = 0;
382 goto end;
383 default:
384 usage(stderr);
385 ret = 1;
386 goto error;
387 }
388 }
389
390 /* If both options are specified, quiet wins */
391 if (lttng_opt_verbose && lttng_opt_quiet) {
392 lttng_opt_verbose = 0;
393 }
394
395 /* No leftovers, print usage and quit */
396 if ((argc - optind) == 0) {
397 usage(stderr);
398 ret = 1;
399 goto error;
400 }
401
402 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
403 user = getenv("USER");
404 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
405 strncmp("compudj", user, 7) == 0))) {
406 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
407 }
408 /* Thanks Mathieu */
409
410 /*
411 * Handle leftovers which is a first level command with the trailing
412 * options.
413 */
414 ret = handle_command(argc - optind, argv + optind);
415 switch (ret) {
416 case CMD_WARNING:
417 WARN("Some command(s) went wrong");
418 break;
419 case CMD_ERROR:
420 ERR("Command error");
421 break;
422 case CMD_UNDEFINED:
423 ERR("Undefined command");
424 break;
425 case CMD_FATAL:
426 ERR("Fatal error");
427 break;
428 case CMD_UNSUPPORTED:
429 ERR("Unsupported command");
430 break;
431 case -1:
432 usage(stderr);
433 ret = 1;
434 break;
435 case 0:
436 break;
437 default:
438 if (ret < 0) {
439 ret = -ret;
440 }
441 break;
442 }
443
444 end:
445 error:
446 return ret;
447 }
448
449
450 /*
451 * main
452 */
453 int main(int argc, char *argv[])
454 {
455 int ret;
456
457 progname = argv[0] ? argv[0] : "lttng";
458
459 ret = set_signal_handler();
460 if (ret < 0) {
461 clean_exit(ret);
462 }
463
464 ret = parse_args(argc, argv);
465 if (ret != 0) {
466 clean_exit(ret);
467 }
468
469 return 0;
470 }
This page took 0.038236 seconds and 3 git commands to generate.