Load: expose overrides elements in mi
[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>
f67353ba 32#include <common/utils.h>
fac6795d 33
c399183f 34#include "command.h"
fac6795d
DG
35
36/* Variables */
37static char *progname;
92360082
JG
38int opt_no_sessiond;
39char *opt_sessiond_path;
f3ed775e 40
8960e9cd
DG
41char *opt_relayd_path;
42
f3ed775e 43enum {
8960e9cd 44 OPT_RELAYD_PATH,
f3ed775e 45 OPT_SESSION_PATH,
865abf65
SM
46 OPT_DUMP_OPTIONS,
47 OPT_DUMP_COMMANDS,
f3ed775e
DG
48};
49
50/* Getopt options. No first level command. */
51static struct option long_options[] = {
70318c38 52 {"version", 0, NULL, 'V'},
f3ed775e
DG
53 {"help", 0, NULL, 'h'},
54 {"group", 1, NULL, 'g'},
55 {"verbose", 0, NULL, 'v'},
56 {"quiet", 0, NULL, 'q'},
c7e35b03 57 {"mi", 1, NULL, 'm'},
8490ae7b 58 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 59 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 60 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
61 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
62 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
63 {NULL, 0, NULL, 0}
64};
65
66/* First level command */
67static struct cmd_struct commands[] = {
3ee1477a 68 { "add-context", cmd_add_context},
f3ed775e
DG
69 { "create", cmd_create},
70 { "destroy", cmd_destroy},
3ee1477a 71 { "disable-channel", cmd_disable_channels},
e953ef25 72 { "disable-event", cmd_disable_events},
d36b8583 73 { "enable-channel", cmd_enable_channels},
3ee1477a
PP
74 { "enable-event", cmd_enable_events},
75 { "help", NULL},
76 { "list", cmd_list},
77 { "load", cmd_load},
78 { "metadata", cmd_metadata},
79 { "save", cmd_save},
3087ef18 80 { "set-session", cmd_set_session},
57f272ed 81 { "snapshot", cmd_snapshot},
3ee1477a
PP
82 { "start", cmd_start},
83 { "status", cmd_status},
84 { "stop", cmd_stop},
ccf10263
MD
85 { "track", cmd_track},
86 { "untrack", cmd_untrack},
960afba4 87 { "help", NULL},
3ee1477a
PP
88 { "version", cmd_version},
89 { "view", cmd_view},
eded6438 90 { "regenerate", cmd_regenerate},
f3ed775e
DG
91 { NULL, NULL} /* Array closure */
92};
93
70318c38
SS
94static void version(FILE *ofp)
95{
4c6ac053
MD
96 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
97 progname,
98 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
70318c38
SS
99}
100
c7e35b03
JR
101/*
102 * Find the MI output type enum from a string. This function is for the support
103 * of machine interface output.
104 */
105static int mi_output_type(const char *output_type)
106{
107 int ret = 0;
108
109 if (!strncasecmp("xml", output_type, 3)) {
110 ret = LTTNG_MI_XML;
111 } else {
112 /* Invalid output format */
113 ERR("MI output format not supported");
114 ret = -LTTNG_ERR_MI_OUTPUT_TYPE;
115 }
116
117 return ret;
118}
119
865abf65
SM
120/*
121 * list_options
122 *
123 * List options line by line. This is mostly for bash auto completion and to
124 * avoid difficult parsing.
125 */
126static void list_options(FILE *ofp)
127{
128 int i = 0;
129 struct option *option = NULL;
130
131 option = &long_options[i];
132 while (option->name != NULL) {
133 fprintf(ofp, "--%s\n", option->name);
134
135 if (isprint(option->val)) {
136 fprintf(ofp, "-%c\n", option->val);
137 }
138
139 i++;
140 option = &long_options[i];
141 }
142}
143
7442b2ba 144/*
f3ed775e 145 * clean_exit
96243366 146 */
f3ed775e 147static void clean_exit(int code)
96243366 148{
f3ed775e
DG
149 DBG("Clean exit");
150 exit(code);
894be886 151}
96243366 152
894be886 153/*
f3ed775e 154 * sighandler
2ef84c95 155 *
f3ed775e 156 * Signal handler for the daemon
2ef84c95 157 */
f3ed775e 158static void sighandler(int sig)
2ef84c95 159{
f3ed775e
DG
160 switch (sig) {
161 case SIGTERM:
99bab54f 162 DBG("SIGTERM caught");
f3ed775e
DG
163 clean_exit(EXIT_FAILURE);
164 break;
f3ed775e 165 default:
99bab54f 166 DBG("Unknown signal %d caught", sig);
f3ed775e 167 break;
2ef84c95
DG
168 }
169
f3ed775e 170 return;
2ef84c95
DG
171}
172
173/*
f3ed775e 174 * set_signal_handler
894be886 175 *
f3ed775e 176 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 177 */
f3ed775e 178static int set_signal_handler(void)
894be886 179{
f3ed775e
DG
180 int ret = 0;
181 struct sigaction sa;
182 sigset_t sigset;
33a2b854 183
f3ed775e 184 if ((ret = sigemptyset(&sigset)) < 0) {
6f04ed72 185 PERROR("sigemptyset");
33a2b854
DG
186 goto end;
187 }
188
f3ed775e
DG
189 sa.sa_handler = sighandler;
190 sa.sa_mask = sigset;
191 sa.sa_flags = 0;
96243366 192
f3ed775e 193 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
6f04ed72 194 PERROR("sigaction");
f3ed775e 195 goto end;
894be886
DG
196 }
197
96243366 198end:
57167058
DG
199 return ret;
200}
201
fac6795d 202/*
f3ed775e 203 * handle_command
fac6795d 204 *
f3ed775e
DG
205 * Handle the full argv list of a first level command. Will find the command
206 * in the global commands array and call the function callback associated.
1c9f7941 207 *
f3ed775e
DG
208 * If command not found, return -1
209 * else, return function command error code.
1c9f7941 210 */
f3ed775e 211static int handle_command(int argc, char **argv)
1c9f7941 212{
f3ed775e
DG
213 int i = 0, ret;
214 struct cmd_struct *cmd;
1c9f7941 215
f3ed775e
DG
216 if (*argv == NULL) {
217 ret = CMD_SUCCESS;
47b74d63 218 goto end;
1c9f7941
DG
219 }
220
960afba4
PP
221 /* Special case for help command which needs the commands array */
222 if (strcmp(argv[0], "help") == 0) {
223 ret = cmd_help(argc, (const char**) argv, commands);
224 goto end;
225 }
226
f3ed775e 227 cmd = &commands[i];
960afba4 228 while (cmd->name != NULL) {
f3ed775e
DG
229 /* Find command */
230 if (strcmp(argv[0], cmd->name) == 0) {
231 ret = cmd->func(argc, (const char**) argv);
f3ed775e 232 goto end;
894be886 233 }
f3ed775e
DG
234 i++;
235 cmd = &commands[i];
894be886
DG
236 }
237
f3ed775e 238 /* Command not found */
4ce78777 239 ret = CMD_UNDEFINED;
894be886
DG
240
241end:
f3ed775e 242 return ret;
8548ff30
DG
243}
244
9fb6c2db
PP
245static void show_basic_help(void)
246{
247 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
248 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
249 puts("");
250 puts("Available commands:");
251 puts("");
252 puts("Tracing sessions:");
253 puts(" create " CONFIG_CMD_DESCR_CREATE);
254 puts(" destroy " CONFIG_CMD_DESCR_DESTROY);
255 puts(" load " CONFIG_CMD_DESCR_LOAD);
256 puts(" metadata " CONFIG_CMD_DESCR_METADATA);
257 puts(" save " CONFIG_CMD_DESCR_SAVE);
258 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION);
259 puts("");
260 puts("Channels:");
261 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT);
262 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL);
263 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL);
264 puts("");
265 puts("Event rules:");
266 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT);
267 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT);
268 puts("");
269 puts("Status:");
270 puts(" list " CONFIG_CMD_DESCR_LIST);
271 puts(" status " CONFIG_CMD_DESCR_STATUS);
272 puts("");
273 puts("Control:");
274 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT);
275 puts(" start " CONFIG_CMD_DESCR_START);
276 puts(" stop " CONFIG_CMD_DESCR_STOP);
277 puts("");
278 puts("Resource tracking:");
279 puts(" track " CONFIG_CMD_DESCR_TRACK);
280 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK);
281 puts("");
282 puts("Miscellaneous:");
9fb6c2db
PP
283 puts(" help " CONFIG_CMD_DESCR_HELP);
284 puts(" version " CONFIG_CMD_DESCR_VERSION);
285 puts(" view " CONFIG_CMD_DESCR_VIEW);
286 puts("");
287 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
288 puts("command COMMAND.");
289 puts("");
290 puts("See `man lttng` for more help with the lttng command.");
291}
292
5b8719f5 293/*
3183dbb0 294 * Parse command line arguments.
5b8719f5 295 *
3183dbb0 296 * Return 0 if OK, else -1
5b8719f5 297 */
f3ed775e 298static int parse_args(int argc, char **argv)
5b8719f5 299{
f3ed775e 300 int opt, ret;
5b8719f5 301
e8fa9fb0
MD
302 if (lttng_is_setuid_setgid()) {
303 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]);
304 clean_exit(EXIT_FAILURE);
305 }
306
f3ed775e 307 if (argc < 2) {
9fb6c2db 308 show_basic_help();
f3ed775e 309 clean_exit(EXIT_FAILURE);
5b8719f5
DG
310 }
311
c7e35b03 312 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
f3ed775e 313 switch (opt) {
70318c38
SS
314 case 'V':
315 version(stdout);
316 ret = 0;
317 goto end;
f3ed775e 318 case 'h':
f67353ba
PP
319 ret = utils_show_man_page(1, "lttng");
320
321 if (ret) {
322 ERR("Cannot view man page lttng(1)");
323 perror("exec");
324 }
3183dbb0 325 goto end;
f3ed775e 326 case 'v':
849e5b7b
DG
327 /* There is only 3 possible level of verbosity. (-vvv) */
328 if (lttng_opt_verbose < 3) {
329 lttng_opt_verbose += 1;
330 }
f3ed775e
DG
331 break;
332 case 'q':
97e19046 333 lttng_opt_quiet = 1;
f3ed775e 334 break;
c7e35b03
JR
335 case 'm':
336 lttng_opt_mi = mi_output_type(optarg);
337 if (lttng_opt_mi < 0) {
338 ret = lttng_opt_mi;
339 goto error;
340 }
341 break;
f3ed775e
DG
342 case 'g':
343 lttng_set_tracing_group(optarg);
344 break;
8490ae7b 345 case 'n':
f3ed775e
DG
346 opt_no_sessiond = 1;
347 break;
348 case OPT_SESSION_PATH:
3ddaa633 349 free(opt_sessiond_path);
f3ed775e 350 opt_sessiond_path = strdup(optarg);
d7c5d39f
MD
351 if (!opt_sessiond_path) {
352 ret = -1;
353 goto error;
354 }
f3ed775e 355 break;
8960e9cd 356 case OPT_RELAYD_PATH:
3ddaa633 357 free(opt_relayd_path);
8960e9cd 358 opt_relayd_path = strdup(optarg);
d7c5d39f
MD
359 if (!opt_relayd_path) {
360 ret = -1;
361 goto error;
362 }
8960e9cd 363 break;
865abf65
SM
364 case OPT_DUMP_OPTIONS:
365 list_options(stdout);
366 ret = 0;
3183dbb0 367 goto end;
865abf65 368 case OPT_DUMP_COMMANDS:
3c9bd23c 369 list_commands(commands, stdout);
865abf65 370 ret = 0;
3183dbb0 371 goto end;
f3ed775e 372 default:
ae856491 373 ret = 1;
f3ed775e
DG
374 goto error;
375 }
5b8719f5 376 }
fac6795d 377
f3ed775e 378 /* If both options are specified, quiet wins */
97e19046
DG
379 if (lttng_opt_verbose && lttng_opt_quiet) {
380 lttng_opt_verbose = 0;
5b8719f5
DG
381 }
382
f67353ba 383 /* No leftovers, quit */
f3ed775e 384 if ((argc - optind) == 0) {
8005f29a 385 ret = 1;
f3ed775e
DG
386 goto error;
387 }
7442b2ba 388
92360082 389 /*
f3ed775e
DG
390 * Handle leftovers which is a first level command with the trailing
391 * options.
392 */
393 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
394 switch (ret) {
395 case CMD_WARNING:
396 WARN("Some command(s) went wrong");
397 break;
398 case CMD_ERROR:
399 ERR("Command error");
400 break;
401 case CMD_UNDEFINED:
f0f2dce9 402 ERR("Undefined command or invalid arguments");
ae856491
DG
403 break;
404 case CMD_FATAL:
405 ERR("Fatal error");
406 break;
4ce78777
DG
407 case CMD_UNSUPPORTED:
408 ERR("Unsupported command");
409 break;
ae856491 410 case -1:
ae856491
DG
411 ret = 1;
412 break;
413 case 0:
414 break;
415 default:
42224349
DG
416 if (ret < 0) {
417 ret = -ret;
418 }
ae856491 419 break;
96243366
DG
420 }
421
3183dbb0 422end:
f3ed775e 423error:
ae856491 424 return ret;
fac6795d
DG
425}
426
f3ed775e 427
fac6795d 428/*
5b8719f5 429 * main
fac6795d
DG
430 */
431int main(int argc, char *argv[])
432{
433 int ret;
434
435 progname = argv[0] ? argv[0] : "lttng";
436
5b8719f5
DG
437 ret = set_signal_handler();
438 if (ret < 0) {
87378cf5 439 clean_exit(ret);
5b8719f5
DG
440 }
441
f3ed775e 442 ret = parse_args(argc, argv);
ae856491
DG
443 if (ret != 0) {
444 clean_exit(ret);
1fff1faa 445 }
87378cf5 446
fac6795d
DG
447 return 0;
448}
This page took 0.069223 seconds and 4 git commands to generate.