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