2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
25 #include <sys/types.h>
29 #include <common/mi-lttng.h>
31 #include "../command.h"
33 static char *opt_event_list
;
34 static int opt_kernel
;
35 static char *opt_channel_name
;
36 static char *opt_session_name
;
37 static int opt_userspace
;
38 static int opt_disable_all
;
41 static int opt_python
;
42 static int opt_event_type
;
54 static struct lttng_handle
*handle
;
55 static struct mi_writer
*writer
;
57 static struct poptOption long_options
[] = {
58 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
59 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
60 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
61 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
62 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
63 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
64 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
65 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
66 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
67 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
68 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_TYPE_SYSCALL
, 0, 0},
69 {"probe", 0, POPT_ARG_NONE
, 0, OPT_TYPE_PROBE
, 0, 0},
70 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TYPE_TRACEPOINT
, 0, 0},
71 {"function", 0, POPT_ARG_NONE
, 0, OPT_TYPE_FUNCTION
, 0, 0},
72 {"all", 0, POPT_ARG_NONE
, 0, OPT_TYPE_ALL
, 0, 0},
73 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
80 static void usage(FILE *ofp
)
82 fprintf(ofp
, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u | -j | -l | -p) [OPTIONS]\n");
84 fprintf(ofp
, "Options:\n");
85 fprintf(ofp
, " -h, --help Show this help\n");
86 fprintf(ofp
, " --list-options Simple listing of options\n");
87 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
88 fprintf(ofp
, " -c, --channel NAME Apply to this channel\n");
89 fprintf(ofp
, " -a, --all-events Disable all tracepoints\n");
90 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
91 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
92 fprintf(ofp
, " -j, --jul Apply to Java application using JUL\n");
93 fprintf(ofp
, " -l, --log4j Apply to Java application using LOG4j\n");
94 fprintf(ofp
, " -p, --python Apply to Python application using logging\n");
96 fprintf(ofp
, "Event type options (Only supported with kernel domain):\n");
97 fprintf(ofp
, " --all All event types (default)\n");
98 fprintf(ofp
, " --tracepoint Tracepoint event\n");
99 fprintf(ofp
, " --syscall System call event\n");
100 fprintf(ofp
, " --probe Probe event\n");
101 fprintf(ofp
, " --function Function event\n");
106 const char *print_channel_name(const char *name
)
108 return name
? : DEFAULT_CHANNEL_NAME
;
112 const char *print_raw_channel_name(const char *name
)
114 return name
? : "<default>";
118 const char *print_event_type(const enum lttng_event_type ev_type
)
121 case LTTNG_EVENT_ALL
:
123 case LTTNG_EVENT_TRACEPOINT
:
125 case LTTNG_EVENT_PROBE
:
127 case LTTNG_EVENT_FUNCTION
:
129 case LTTNG_EVENT_FUNCTION_ENTRY
:
130 return "function entry";
131 case LTTNG_EVENT_SYSCALL
:
138 /* Mi print a partial event.
142 static int mi_print_event(char *event_name
, int enabled
, int success
)
149 /* Open event element */
150 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
155 /* Print the name of event */
156 ret
= mi_lttng_writer_write_element_string(writer
,
157 config_element_name
, event_name
);
162 /* Print enabled ? */
163 ret
= mi_lttng_writer_write_element_bool(writer
,
164 config_element_enabled
, enabled
);
170 ret
= mi_lttng_writer_write_element_bool(writer
,
171 mi_lttng_element_command_success
, success
);
176 /* Close event element */
177 ret
= mi_lttng_writer_close_element(writer
);
185 * Disabling event using the lttng API.
187 static int disable_events(char *session_name
)
189 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
190 int enabled
= 1, success
= 1;
191 char *event_name
, *channel_name
= NULL
;
192 struct lttng_domain dom
;
193 struct lttng_event event
;
195 memset(&dom
, 0, sizeof(dom
));
197 /* Create lttng domain */
199 dom
.type
= LTTNG_DOMAIN_KERNEL
;
200 } else if (opt_userspace
) {
201 dom
.type
= LTTNG_DOMAIN_UST
;
202 } else if (opt_jul
) {
203 dom
.type
= LTTNG_DOMAIN_JUL
;
204 } else if (opt_log4j
) {
205 dom
.type
= LTTNG_DOMAIN_LOG4J
;
206 } else if (opt_python
) {
207 dom
.type
= LTTNG_DOMAIN_PYTHON
;
209 /* Checked by the caller. */
213 channel_name
= opt_channel_name
;
215 handle
= lttng_create_handle(session_name
, &dom
);
216 if (handle
== NULL
) {
221 /* Mi print the channel and open the events element */
223 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
229 ret
= mi_lttng_writer_write_element_string(writer
,
230 config_element_name
, print_channel_name(channel_name
));
236 /* Open events element */
237 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
244 memset(&event
, 0, sizeof(event
));
245 /* Set default loglevel to any/unknown */
248 /* opt_event_type contain the event type to disable at this point */
249 event
.type
= opt_event_type
;
251 if (opt_disable_all
) {
252 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
253 if (command_ret
< 0) {
254 ERR("%s", lttng_strerror(command_ret
));
261 MSG("All %s events of type %s are disabled in channel %s",
262 get_domain_str(dom
.type
),
263 print_event_type(opt_event_type
),
264 print_channel_name(channel_name
));
268 ret
= mi_print_event("*", enabled
, success
);
275 /* Strip event list */
276 event_name
= strtok(opt_event_list
, ",");
277 while (event_name
!= NULL
) {
278 DBG("Disabling event %s", event_name
);
280 strncpy(event
.name
, event_name
, sizeof(event
.name
));
281 event
.name
[sizeof(event
.name
) - 1] = '\0';
282 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
283 if (command_ret
< 0) {
284 ERR("%s of type %s : %s (channel %s, session %s)",
286 print_event_type(opt_event_type
),
287 lttng_strerror(command_ret
),
288 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
289 ? print_raw_channel_name(channel_name
)
290 : print_channel_name(channel_name
),
295 * If an error occurred we assume that the event is still
300 MSG("%s %s of type %s disabled in channel %s for session %s",
301 get_domain_str(dom
.type
),
303 print_event_type(opt_event_type
),
304 print_channel_name(channel_name
),
311 ret
= mi_print_event(event_name
, enabled
, success
);
319 event_name
= strtok(NULL
, ",");
325 /* Close events element and channel element */
326 ret
= mi_lttng_close_multi_element(writer
, 2);
332 /* if there is already an error preserve it */
337 /* Overwrite ret if an error occurred */
338 ret
= command_ret
? command_ret
: ret
;
340 lttng_destroy_handle(handle
);
347 * Disable event to trace session
349 int cmd_disable_events(int argc
, const char **argv
)
351 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
352 static poptContext pc
;
353 char *session_name
= NULL
;
356 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
357 poptReadDefaultConfig(pc
, 0);
359 /* Default event type */
360 opt_event_type
= LTTNG_EVENT_ALL
;
362 while ((opt
= poptGetNextOpt(pc
)) != -1) {
367 case OPT_TYPE_SYSCALL
:
368 opt_event_type
= LTTNG_EVENT_SYSCALL
;
370 case OPT_TYPE_TRACEPOINT
:
371 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
374 opt_event_type
= LTTNG_EVENT_PROBE
;
376 case OPT_TYPE_FUNCTION
:
377 opt_event_type
= LTTNG_EVENT_FUNCTION
;
380 opt_event_type
= LTTNG_EVENT_ALL
;
382 case OPT_LIST_OPTIONS
:
383 list_cmd_options(stdout
, long_options
);
391 /* Validate event type. Multiple event type are not supported. */
392 if (event_type
== -1) {
393 event_type
= opt_event_type
;
395 if (event_type
!= opt_event_type
) {
396 ERR("Multiple event type not supported.");
403 ret
= print_missing_or_multiple_domains(
404 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
410 /* Ust and agent only support ALL event type */
411 if ((opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
)
412 && opt_event_type
!= LTTNG_EVENT_ALL
) {
413 ERR("UST and agent (-j | -l | -p) event(s) disabling based on event type is not supported.\n");
419 opt_event_list
= (char*) poptGetArg(pc
);
420 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
421 ERR("Missing event name(s).\n");
427 if (!opt_session_name
) {
428 session_name
= get_session_name();
429 if (session_name
== NULL
) {
434 session_name
= opt_session_name
;
439 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
441 ret
= -LTTNG_ERR_NOMEM
;
445 /* Open command element */
446 ret
= mi_lttng_writer_command_open(writer
,
447 mi_lttng_element_command_disable_event
);
453 /* Open output element */
454 ret
= mi_lttng_writer_open_element(writer
,
455 mi_lttng_element_command_output
);
462 command_ret
= disable_events(session_name
);
469 /* Close output element */
470 ret
= mi_lttng_writer_close_element(writer
);
476 ret
= mi_lttng_writer_write_element_bool(writer
,
477 mi_lttng_element_command_success
, success
);
483 /* Command element close */
484 ret
= mi_lttng_writer_command_close(writer
);
492 if (!opt_session_name
&& session_name
) {
497 if (writer
&& mi_lttng_writer_destroy(writer
)) {
498 /* Preserve original error code */
499 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
502 /* Overwrite ret if an error occured in disable_events */
503 ret
= command_ret
? command_ret
: ret
;