lttng: show man page when using command's --help
[lttng-tools.git] / src / bin / lttng / commands / disable_events.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 <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <assert.h>
27
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32 static char *opt_event_list;
33 static int opt_kernel;
34 static char *opt_channel_name;
35 static char *opt_session_name;
36 static int opt_userspace;
37 static int opt_disable_all;
38 static int opt_jul;
39 static int opt_log4j;
40 static int opt_python;
41 static int opt_event_type;
42
43 enum {
44 OPT_HELP = 1,
45 OPT_TYPE_SYSCALL,
46 OPT_TYPE_TRACEPOINT,
47 OPT_TYPE_PROBE,
48 OPT_TYPE_FUNCTION,
49 OPT_TYPE_ALL,
50 OPT_LIST_OPTIONS,
51 };
52
53 static struct lttng_handle *handle;
54 static struct mi_writer *writer;
55
56 static struct poptOption long_options[] = {
57 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
58 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
59 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
60 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
61 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
62 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
63 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
64 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
65 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
66 {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0},
67 {"syscall", 0, POPT_ARG_NONE, 0, OPT_TYPE_SYSCALL, 0, 0},
68 {"probe", 0, POPT_ARG_NONE, 0, OPT_TYPE_PROBE, 0, 0},
69 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TYPE_TRACEPOINT, 0, 0},
70 {"function", 0, POPT_ARG_NONE, 0, OPT_TYPE_FUNCTION, 0, 0},
71 {"all", 0, POPT_ARG_NONE, 0, OPT_TYPE_ALL, 0, 0},
72 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
73 {0, 0, 0, 0, 0, 0, 0}
74 };
75
76 /*
77 * usage
78 */
79 static void usage(FILE *ofp)
80 {
81 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u | -j | -l | -p) [OPTIONS]\n");
82 fprintf(ofp, "\n");
83 fprintf(ofp, "Options:\n");
84 fprintf(ofp, " -h, --help Show this help\n");
85 fprintf(ofp, " --list-options Simple listing of options\n");
86 fprintf(ofp, " -s, --session NAME Apply to session name\n");
87 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
88 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
89 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
90 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
91 fprintf(ofp, " -j, --jul Apply to Java application using JUL\n");
92 fprintf(ofp, " -l, --log4j Apply to Java application using LOG4j\n");
93 fprintf(ofp, " -p, --python Apply to Python application using logging\n");
94 fprintf(ofp, "\n");
95 fprintf(ofp, "Event type options (Only supported with kernel domain):\n");
96 fprintf(ofp, " --all All event types (default)\n");
97 fprintf(ofp, " --tracepoint Tracepoint event\n");
98 fprintf(ofp, " --syscall System call event\n");
99 fprintf(ofp, " --probe Probe event\n");
100 fprintf(ofp, " --function Function event\n");
101 fprintf(ofp, "\n");
102 }
103
104 static
105 const char *print_channel_name(const char *name)
106 {
107 return name ? : DEFAULT_CHANNEL_NAME;
108 }
109
110 static
111 const char *print_raw_channel_name(const char *name)
112 {
113 return name ? : "<default>";
114 }
115
116 static
117 const char *print_event_type(const enum lttng_event_type ev_type)
118 {
119 switch (ev_type) {
120 case LTTNG_EVENT_ALL:
121 return "any";
122 case LTTNG_EVENT_TRACEPOINT:
123 return "tracepoint";
124 case LTTNG_EVENT_PROBE:
125 return "probe";
126 case LTTNG_EVENT_FUNCTION:
127 return "function";
128 case LTTNG_EVENT_FUNCTION_ENTRY:
129 return "function entry";
130 case LTTNG_EVENT_SYSCALL:
131 return "syscall";
132 default:
133 return "";
134 }
135 }
136
137 /* Mi print a partial event.
138 * enabled is 0 or 1
139 * success is 0 or 1
140 */
141 static int mi_print_event(char *event_name, int enabled, int success)
142 {
143 int ret;
144
145 assert(writer);
146 assert(event_name);
147
148 /* Open event element */
149 ret = mi_lttng_writer_open_element(writer, config_element_event);
150 if (ret) {
151 goto end;
152 }
153
154 /* Print the name of event */
155 ret = mi_lttng_writer_write_element_string(writer,
156 config_element_name, event_name);
157 if (ret) {
158 goto end;
159 }
160
161 /* Print enabled ? */
162 ret = mi_lttng_writer_write_element_bool(writer,
163 config_element_enabled, enabled);
164 if (ret) {
165 goto end;
166 }
167
168 /* Success ? */
169 ret = mi_lttng_writer_write_element_bool(writer,
170 mi_lttng_element_command_success, success);
171 if (ret) {
172 goto end;
173 }
174
175 /* Close event element */
176 ret = mi_lttng_writer_close_element(writer);
177 end:
178 return ret;
179 }
180
181 /*
182 * disable_events
183 *
184 * Disabling event using the lttng API.
185 */
186 static int disable_events(char *session_name)
187 {
188 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
189 int enabled = 1, success = 1;
190 char *event_name, *channel_name = NULL;
191 struct lttng_domain dom;
192 struct lttng_event event;
193
194 memset(&dom, 0, sizeof(dom));
195
196 /* Create lttng domain */
197 if (opt_kernel) {
198 dom.type = LTTNG_DOMAIN_KERNEL;
199 } else if (opt_userspace) {
200 dom.type = LTTNG_DOMAIN_UST;
201 } else if (opt_jul) {
202 dom.type = LTTNG_DOMAIN_JUL;
203 } else if (opt_log4j) {
204 dom.type = LTTNG_DOMAIN_LOG4J;
205 } else if (opt_python) {
206 dom.type = LTTNG_DOMAIN_PYTHON;
207 } else {
208 /* Checked by the caller. */
209 assert(0);
210 }
211
212 channel_name = opt_channel_name;
213
214 handle = lttng_create_handle(session_name, &dom);
215 if (handle == NULL) {
216 ret = -1;
217 goto error;
218 }
219
220 /* Mi print the channel and open the events element */
221 if (lttng_opt_mi) {
222 ret = mi_lttng_writer_open_element(writer, config_element_channel);
223 if (ret) {
224 ret = CMD_ERROR;
225 goto end;
226 }
227
228 ret = mi_lttng_writer_write_element_string(writer,
229 config_element_name, print_channel_name(channel_name));
230 if (ret) {
231 ret = CMD_ERROR;
232 goto end;
233 }
234
235 /* Open events element */
236 ret = mi_lttng_writer_open_element(writer, config_element_events);
237 if (ret) {
238 ret = CMD_ERROR;
239 goto end;
240 }
241 }
242
243 memset(&event, 0, sizeof(event));
244 /* Set default loglevel to any/unknown */
245 event.loglevel = -1;
246
247 /* opt_event_type contain the event type to disable at this point */
248 event.type = opt_event_type;
249
250 if (opt_disable_all) {
251 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
252 if (command_ret < 0) {
253 ERR("%s", lttng_strerror(command_ret));
254 enabled = 1;
255 success = 0;
256
257 } else {
258 enabled = 0;
259 success = 1;
260 MSG("All %s events of type %s are disabled in channel %s",
261 get_domain_str(dom.type),
262 print_event_type(opt_event_type),
263 print_channel_name(channel_name));
264 }
265
266 if (lttng_opt_mi) {
267 ret = mi_print_event("*", enabled, success);
268 if (ret) {
269 ret = CMD_ERROR;
270 goto error;
271 }
272 }
273 } else {
274 /* Strip event list */
275 event_name = strtok(opt_event_list, ",");
276 while (event_name != NULL) {
277 DBG("Disabling event %s", event_name);
278
279 strncpy(event.name, event_name, sizeof(event.name));
280 event.name[sizeof(event.name) - 1] = '\0';
281 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
282 if (command_ret < 0) {
283 ERR("%s of type %s : %s (channel %s, session %s)",
284 event_name,
285 print_event_type(opt_event_type),
286 lttng_strerror(command_ret),
287 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
288 ? print_raw_channel_name(channel_name)
289 : print_channel_name(channel_name),
290 session_name);
291 warn = 1;
292 success = 0;
293 /*
294 * If an error occurred we assume that the event is still
295 * enabled.
296 */
297 enabled = 1;
298 } else {
299 MSG("%s %s of type %s disabled in channel %s for session %s",
300 get_domain_str(dom.type),
301 event_name,
302 print_event_type(opt_event_type),
303 print_channel_name(channel_name),
304 session_name);
305 success = 1;
306 enabled = 0;
307 }
308
309 if (lttng_opt_mi) {
310 ret = mi_print_event(event_name, enabled, success);
311 if (ret) {
312 ret = CMD_ERROR;
313 goto error;
314 }
315 }
316
317 /* Next event */
318 event_name = strtok(NULL, ",");
319 }
320 }
321
322 end:
323 if (lttng_opt_mi) {
324 /* Close events element and channel element */
325 ret = mi_lttng_close_multi_element(writer, 2);
326 if (ret) {
327 ret = CMD_ERROR;
328 }
329 }
330 error:
331 /* if there is already an error preserve it */
332 if (warn && !ret) {
333 ret = CMD_WARNING;
334 }
335
336 /* Overwrite ret if an error occurred */
337 ret = command_ret ? command_ret : ret;
338
339 lttng_destroy_handle(handle);
340 return ret;
341 }
342
343 /*
344 * cmd_disable_events
345 *
346 * Disable event to trace session
347 */
348 int cmd_disable_events(int argc, const char **argv)
349 {
350 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
351 static poptContext pc;
352 char *session_name = NULL;
353 int event_type = -1;
354
355 pc = poptGetContext(NULL, argc, argv, long_options, 0);
356 poptReadDefaultConfig(pc, 0);
357
358 /* Default event type */
359 opt_event_type = LTTNG_EVENT_ALL;
360
361 while ((opt = poptGetNextOpt(pc)) != -1) {
362 switch (opt) {
363 case OPT_HELP:
364 SHOW_HELP();
365 goto end;
366 case OPT_TYPE_SYSCALL:
367 opt_event_type = LTTNG_EVENT_SYSCALL;
368 break;
369 case OPT_TYPE_TRACEPOINT:
370 opt_event_type = LTTNG_EVENT_TRACEPOINT;
371 break;
372 case OPT_TYPE_PROBE:
373 opt_event_type = LTTNG_EVENT_PROBE;
374 break;
375 case OPT_TYPE_FUNCTION:
376 opt_event_type = LTTNG_EVENT_FUNCTION;
377 break;
378 case OPT_TYPE_ALL:
379 opt_event_type = LTTNG_EVENT_ALL;
380 break;
381 case OPT_LIST_OPTIONS:
382 list_cmd_options(stdout, long_options);
383 goto end;
384 default:
385 usage(stderr);
386 ret = CMD_UNDEFINED;
387 goto end;
388 }
389
390 /* Validate event type. Multiple event type are not supported. */
391 if (event_type == -1) {
392 event_type = opt_event_type;
393 } else {
394 if (event_type != opt_event_type) {
395 ERR("Multiple event type not supported.");
396 ret = CMD_ERROR;
397 goto end;
398 }
399 }
400 }
401
402 ret = print_missing_or_multiple_domains(
403 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
404 if (ret) {
405 ret = CMD_ERROR;
406 goto end;
407 }
408
409 /* Ust and agent only support ALL event type */
410 if ((opt_userspace || opt_jul || opt_log4j || opt_python)
411 && opt_event_type != LTTNG_EVENT_ALL) {
412 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
413 usage(stderr);
414 ret = CMD_ERROR;
415 goto end;
416 }
417
418 opt_event_list = (char*) poptGetArg(pc);
419 if (opt_event_list == NULL && opt_disable_all == 0) {
420 ERR("Missing event name(s).\n");
421 usage(stderr);
422 ret = CMD_ERROR;
423 goto end;
424 }
425
426 if (!opt_session_name) {
427 session_name = get_session_name();
428 if (session_name == NULL) {
429 ret = CMD_ERROR;
430 goto end;
431 }
432 } else {
433 session_name = opt_session_name;
434 }
435
436 /* Mi check */
437 if (lttng_opt_mi) {
438 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
439 if (!writer) {
440 ret = -LTTNG_ERR_NOMEM;
441 goto end;
442 }
443
444 /* Open command element */
445 ret = mi_lttng_writer_command_open(writer,
446 mi_lttng_element_command_disable_event);
447 if (ret) {
448 ret = CMD_ERROR;
449 goto end;
450 }
451
452 /* Open output element */
453 ret = mi_lttng_writer_open_element(writer,
454 mi_lttng_element_command_output);
455 if (ret) {
456 ret = CMD_ERROR;
457 goto end;
458 }
459 }
460
461 command_ret = disable_events(session_name);
462 if (command_ret) {
463 success = 0;
464 }
465
466 /* Mi closing */
467 if (lttng_opt_mi) {
468 /* Close output element */
469 ret = mi_lttng_writer_close_element(writer);
470 if (ret) {
471 ret = CMD_ERROR;
472 goto end;
473 }
474
475 ret = mi_lttng_writer_write_element_bool(writer,
476 mi_lttng_element_command_success, success);
477 if (ret) {
478 ret = CMD_ERROR;
479 goto end;
480 }
481
482 /* Command element close */
483 ret = mi_lttng_writer_command_close(writer);
484 if (ret) {
485 ret = CMD_ERROR;
486 goto end;
487 }
488 }
489
490 end:
491 if (!opt_session_name && session_name) {
492 free(session_name);
493 }
494
495 /* Mi clean-up */
496 if (writer && mi_lttng_writer_destroy(writer)) {
497 /* Preserve original error code */
498 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
499 }
500
501 /* Overwrite ret if an error occured in disable_events */
502 ret = command_ret ? command_ret : ret;
503
504 poptFreeContext(pc);
505 return ret;
506 }
This page took 0.038694 seconds and 4 git commands to generate.