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