Command metadata regenerate
[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},
93ec662e 87 { "metadata", cmd_metadata},
f3ed775e
DG
88 { NULL, NULL} /* Array closure */
89};
90
91static void usage(FILE *ofp)
8c0faa1d 92{
4c6ac053
MD
93 fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n",
94 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
32a6298d 95 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
f3ed775e
DG
96 fprintf(ofp, "\n");
97 fprintf(ofp, "Options:\n");
70318c38 98 fprintf(ofp, " -V, --version Show version\n");
99bab54f
TD
99 fprintf(ofp, " -h, --help Show this help\n");
100 fprintf(ofp, " --list-options Simple listing of lttng options\n");
101 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
102 fprintf(ofp, " -v, --verbose Increase verbosity\n");
103 fprintf(ofp, " -q, --quiet Quiet mode\n");
c7e35b03
JR
104 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
105 fprintf(ofp, " Type: xml\n");
99bab54f
TD
106 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
107 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
108 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
8960e9cd 109 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
f3ed775e
DG
110 fprintf(ofp, "\n");
111 fprintf(ofp, "Commands:\n");
00e2e675
DG
112 fprintf(ofp, " add-context Add context to event and/or channel\n");
113 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
114 fprintf(ofp, " create Create tracing session\n");
115 fprintf(ofp, " destroy Tear down tracing session\n");
116 fprintf(ofp, " enable-channel Enable tracing channel\n");
117 fprintf(ofp, " enable-event Enable tracing event\n");
118 fprintf(ofp, " disable-channel Disable tracing channel\n");
119 fprintf(ofp, " disable-event Disable tracing event\n");
00e2e675
DG
120 fprintf(ofp, " list List possible tracing options\n");
121 fprintf(ofp, " set-session Set current session name\n");
57f272ed 122 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
00e2e675 123 fprintf(ofp, " start Start tracing\n");
54a0adbf 124 fprintf(ofp, " status Show current session's details\n");
00e2e675
DG
125 fprintf(ofp, " stop Stop tracing\n");
126 fprintf(ofp, " version Show version information\n");
127 fprintf(ofp, " view Start trace viewer\n");
c864d6d7 128 fprintf(ofp, " save Save session configuration\n");
8c42d845 129 fprintf(ofp, " load Load session configuration\n");
ccf10263
MD
130 fprintf(ofp, " track Track specific system resources\n");
131 fprintf(ofp, " untrack Untrack specific system resources\n");
93ec662e 132 fprintf(ofp, " metadata Regenerate the metadata of a session\n");
f3ed775e 133 fprintf(ofp, "\n");
99bab54f
TD
134 fprintf(ofp, "Each command also has its own -h, --help option.\n");
135 fprintf(ofp, "\n");
f3ed775e
DG
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");
8c0faa1d
DG
138}
139
70318c38
SS
140static void version(FILE *ofp)
141{
4c6ac053
MD
142 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
143 progname,
144 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
145}
146
c7e35b03
JR
147/*
148 * Find the MI output type enum from a string. This function is for the support
149 * of machine interface output.
150 */
151static 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
865abf65
SM
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 */
172static 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
7442b2ba 190/*
f3ed775e 191 * clean_exit
96243366 192 */
f3ed775e 193static void clean_exit(int code)
96243366 194{
f3ed775e
DG
195 DBG("Clean exit");
196 exit(code);
894be886 197}
96243366 198
894be886 199/*
f3ed775e 200 * sighandler
2ef84c95 201 *
f3ed775e 202 * Signal handler for the daemon
2ef84c95 203 */
f3ed775e 204static void sighandler(int sig)
2ef84c95 205{
f3ed775e
DG
206 switch (sig) {
207 case SIGTERM:
99bab54f 208 DBG("SIGTERM caught");
f3ed775e
DG
209 clean_exit(EXIT_FAILURE);
210 break;
f3ed775e 211 default:
99bab54f 212 DBG("Unknown signal %d caught", sig);
f3ed775e 213 break;
2ef84c95
DG
214 }
215
f3ed775e 216 return;
2ef84c95
DG
217}
218
219/*
f3ed775e 220 * set_signal_handler
894be886 221 *
f3ed775e 222 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 223 */
f3ed775e 224static int set_signal_handler(void)
894be886 225{
f3ed775e
DG
226 int ret = 0;
227 struct sigaction sa;
228 sigset_t sigset;
33a2b854 229
f3ed775e 230 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 231 PERROR("sigemptyset");
33a2b854
DG
232 goto end;
233 }
234
f3ed775e
DG
235 sa.sa_handler = sighandler;
236 sa.sa_mask = sigset;
237 sa.sa_flags = 0;
96243366 238
f3ed775e 239 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 240 PERROR("sigaction");
f3ed775e 241 goto end;
894be886
DG
242 }
243
96243366 244end:
57167058
DG
245 return ret;
246}
247
fac6795d 248/*
f3ed775e 249 * handle_command
fac6795d 250 *
f3ed775e
DG
251 * Handle the full argv list of a first level command. Will find the command
252 * in the global commands array and call the function callback associated.
1c9f7941 253 *
f3ed775e
DG
254 * If command not found, return -1
255 * else, return function command error code.
1c9f7941 256 */
f3ed775e 257static int handle_command(int argc, char **argv)
1c9f7941 258{
f3ed775e
DG
259 int i = 0, ret;
260 struct cmd_struct *cmd;
1c9f7941 261
f3ed775e
DG
262 if (*argv == NULL) {
263 ret = CMD_SUCCESS;
47b74d63 264 goto end;
1c9f7941
DG
265 }
266
f3ed775e
DG
267 cmd = &commands[i];
268 while (cmd->func != NULL) {
269 /* Find command */
270 if (strcmp(argv[0], cmd->name) == 0) {
271 ret = cmd->func(argc, (const char**) argv);
f3ed775e 272 goto end;
894be886 273 }
f3ed775e
DG
274 i++;
275 cmd = &commands[i];
894be886
DG
276 }
277
f3ed775e 278 /* Command not found */
4ce78777 279 ret = CMD_UNDEFINED;
894be886
DG
280
281end:
f3ed775e 282 return ret;
8548ff30
DG
283}
284
5b8719f5 285/*
3183dbb0 286 * Parse command line arguments.
5b8719f5 287 *
3183dbb0 288 * Return 0 if OK, else -1
5b8719f5 289 */
f3ed775e 290static int parse_args(int argc, char **argv)
5b8719f5 291{
f3ed775e 292 int opt, ret;
883d80f9 293 char *user;
5b8719f5 294
e8fa9fb0
MD
295 if (lttng_is_setuid_setgid()) {
296 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
297 clean_exit(EXIT_FAILURE);
298 }
299
f3ed775e
DG
300 if (argc < 2) {
301 usage(stderr);
302 clean_exit(EXIT_FAILURE);
5b8719f5
DG
303 }
304
c7e35b03 305 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 306 switch (opt) {
70318c38
SS
307 case 'V':
308 version(stdout);
309 ret = 0;
310 goto end;
f3ed775e 311 case 'h':
3183dbb0 312 usage(stdout);
ae856491 313 ret = 0;
3183dbb0 314 goto end;
f3ed775e 315 case 'v':
849e5b7b
DG
316 /* There is only 3 possible level of verbosity. (-vvv) */
317 if (lttng_opt_verbose < 3) {
318 lttng_opt_verbose += 1;
319 }
f3ed775e
DG
320 break;
321 case 'q':
97e19046 322 lttng_opt_quiet = 1;
f3ed775e 323 break;
c7e35b03
JR
324 case 'm':
325 lttng_opt_mi = mi_output_type(optarg);
326 if (lttng_opt_mi < 0) {
327 ret = lttng_opt_mi;
328 goto error;
329 }
330 break;
f3ed775e
DG
331 case 'g':
332 lttng_set_tracing_group(optarg);
333 break;
8490ae7b 334 case 'n':
f3ed775e
DG
335 opt_no_sessiond = 1;
336 break;
337 case OPT_SESSION_PATH:
338 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
339 if (!opt_sessiond_path) {
340 ret = -1;
341 goto error;
342 }
f3ed775e 343 break;
8960e9cd
DG
344 case OPT_RELAYD_PATH:
345 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
346 if (!opt_relayd_path) {
347 ret = -1;
348 goto error;
349 }
8960e9cd 350 break;
865abf65
SM
351 case OPT_DUMP_OPTIONS:
352 list_options(stdout);
353 ret = 0;
3183dbb0 354 goto end;
865abf65 355 case OPT_DUMP_COMMANDS:
3c9bd23c 356 list_commands(commands, stdout);
865abf65 357 ret = 0;
3183dbb0 358 goto end;
f3ed775e
DG
359 default:
360 usage(stderr);
ae856491 361 ret = 1;
f3ed775e
DG
362 goto error;
363 }
5b8719f5 364 }
fac6795d 365
f3ed775e 366 /* If both options are specified, quiet wins */
97e19046
DG
367 if (lttng_opt_verbose && lttng_opt_quiet) {
368 lttng_opt_verbose = 0;
5b8719f5
DG
369 }
370
f3ed775e
DG
371 /* No leftovers, print usage and quit */
372 if ((argc - optind) == 0) {
373 usage(stderr);
8005f29a 374 ret = 1;
f3ed775e
DG
375 goto error;
376 }
7442b2ba 377
883d80f9
DG
378 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
379 user = getenv("USER");
380 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
381 strncmp("compudj", user, 7) == 0))) {
382 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
383 }
384 /* Thanks Mathieu */
385
92360082 386 /*
f3ed775e
DG
387 * Handle leftovers which is a first level command with the trailing
388 * options.
389 */
390 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
391 switch (ret) {
392 case CMD_WARNING:
393 WARN("Some command(s) went wrong");
394 break;
395 case CMD_ERROR:
396 ERR("Command error");
397 break;
398 case CMD_UNDEFINED:
399 ERR("Undefined command");
400 break;
401 case CMD_FATAL:
402 ERR("Fatal error");
403 break;
4ce78777
DG
404 case CMD_UNSUPPORTED:
405 ERR("Unsupported command");
406 break;
ae856491
DG
407 case -1:
408 usage(stderr);
409 ret = 1;
410 break;
411 case 0:
412 break;
413 default:
42224349
DG
414 if (ret < 0) {
415 ret = -ret;
416 }
ae856491 417 break;
96243366
DG
418 }
419
3183dbb0 420end:
f3ed775e 421error:
ae856491 422 return ret;
fac6795d
DG
423}
424
f3ed775e 425
fac6795d 426/*
5b8719f5 427 * main
fac6795d
DG
428 */
429int main(int argc, char *argv[])
430{
431 int ret;
432
433 progname = argv[0] ? argv[0] : "lttng";
434
5b8719f5
DG
435 ret = set_signal_handler();
436 if (ret < 0) {
87378cf5 437 clean_exit(ret);
5b8719f5
DG
438 }
439
f3ed775e 440 ret = parse_args(argc, argv);
ae856491
DG
441 if (ret != 0) {
442 clean_exit(ret);
1fff1faa 443 }
87378cf5 444
fac6795d
DG
445 return 0;
446}
This page took 0.080164 seconds and 5 git commands to generate.