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.
24 #include <sys/types.h>
28 #include <common/mi-lttng.h>
30 #include "../command.h"
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
;
40 static int opt_python
;
41 static int opt_event_type
;
43 /* Not implemented yet */
44 static char *opt_cmd_name
;
55 static struct lttng_handle
*handle
;
56 static struct mi_writer
*writer
;
58 static struct poptOption long_options
[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
61 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
62 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
63 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
64 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
65 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
66 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
67 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
68 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
70 /* Not implemented yet */
71 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
72 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
74 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
76 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
83 static void usage(FILE *ofp
)
85 fprintf(ofp
, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
87 fprintf(ofp
, "Options:\n");
88 fprintf(ofp
, " -h, --help Show this help\n");
89 fprintf(ofp
, " --list-options Simple listing of options\n");
90 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
91 fprintf(ofp
, " -c, --channel NAME Apply to this channel\n");
92 fprintf(ofp
, " -a, --all-events Disable all tracepoints\n");
93 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
94 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
95 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
96 fprintf(ofp
, " -l, --log4j Apply to Java application using LOG4j\n");
97 fprintf(ofp
, " -p, --python Apply to Python application using logging\n");
99 fprintf(ofp
, "Event options:\n");
100 fprintf(ofp
, " --syscall System call event\n");
105 const char *print_channel_name(const char *name
)
107 return name
? : DEFAULT_CHANNEL_NAME
;
111 const char *print_raw_channel_name(const char *name
)
113 return name
? : "<default>";
116 /* Mi print a partial event.
120 static int mi_print_event(char *event_name
, int enabled
, int success
)
127 /* Open event element */
128 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
133 /* Print the name of event */
134 ret
= mi_lttng_writer_write_element_string(writer
,
135 config_element_name
, event_name
);
140 /* Print enabled ? */
141 ret
= mi_lttng_writer_write_element_bool(writer
,
142 config_element_enabled
, enabled
);
148 ret
= mi_lttng_writer_write_element_bool(writer
,
149 mi_lttng_element_command_success
, success
);
154 /* Close event element */
155 ret
= mi_lttng_writer_close_element(writer
);
163 * Disabling event using the lttng API.
165 static int disable_events(char *session_name
)
167 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
168 int enabled
= 1, success
= 1;
169 char *event_name
, *channel_name
= NULL
;
170 struct lttng_domain dom
;
171 struct lttng_event event
;
173 memset(&dom
, 0, sizeof(dom
));
175 /* Create lttng domain */
177 dom
.type
= LTTNG_DOMAIN_KERNEL
;
178 } else if (opt_userspace
) {
179 dom
.type
= LTTNG_DOMAIN_UST
;
180 } else if (opt_jul
) {
181 dom
.type
= LTTNG_DOMAIN_JUL
;
182 } else if (opt_log4j
) {
183 dom
.type
= LTTNG_DOMAIN_LOG4J
;
184 } else if (opt_python
) {
185 dom
.type
= LTTNG_DOMAIN_PYTHON
;
187 print_missing_domain();
192 channel_name
= opt_channel_name
;
194 handle
= lttng_create_handle(session_name
, &dom
);
195 if (handle
== NULL
) {
200 /* Mi print the channel and open the events element */
202 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
208 ret
= mi_lttng_writer_write_element_string(writer
,
209 config_element_name
, print_channel_name(channel_name
));
215 /* Open events element */
216 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
223 memset(&event
, 0, sizeof(event
));
224 switch (opt_event_type
) {
225 case LTTNG_EVENT_SYSCALL
:
226 event
.type
= LTTNG_EVENT_SYSCALL
;
229 event
.type
= LTTNG_EVENT_ALL
;
233 if (opt_disable_all
) {
234 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
235 if (command_ret
< 0) {
236 ERR("%s", lttng_strerror(command_ret
));
243 MSG("All %s %s are disabled in channel %s",
244 get_domain_str(dom
.type
),
245 opt_event_type
== LTTNG_EVENT_SYSCALL
? "system calls" : "events",
246 print_channel_name(channel_name
));
250 ret
= mi_print_event("*", enabled
, success
);
257 /* Strip event list */
258 event_name
= strtok(opt_event_list
, ",");
259 while (event_name
!= NULL
) {
260 DBG("Disabling event %s", event_name
);
262 strncpy(event
.name
, event_name
, sizeof(event
.name
));
263 event
.name
[sizeof(event
.name
) - 1] = '\0';
264 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
265 if (command_ret
< 0) {
266 ERR("%s %s: %s (channel %s, session %s)",
267 opt_event_type
== LTTNG_EVENT_SYSCALL
? "System call" : "Event",
269 lttng_strerror(command_ret
),
270 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
271 ? print_raw_channel_name(channel_name
)
272 : print_channel_name(channel_name
),
277 * If an error occurred we assume that the event is still
282 MSG("%s %s %s disabled in channel %s for session %s",
283 get_domain_str(dom
.type
),
284 opt_event_type
== LTTNG_EVENT_SYSCALL
? "system call" : "event",
286 print_channel_name(channel_name
),
293 ret
= mi_print_event(event_name
, enabled
, success
);
301 event_name
= strtok(NULL
, ",");
307 /* Close events element and channel element */
308 ret
= mi_lttng_close_multi_element(writer
, 2);
314 /* if there is already an error preserve it */
319 /* Overwrite ret if an error occurred */
320 ret
= command_ret
? command_ret
: ret
;
322 lttng_destroy_handle(handle
);
329 * Disable event to trace session
331 int cmd_disable_events(int argc
, const char **argv
)
333 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
334 static poptContext pc
;
335 char *session_name
= NULL
;
338 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
339 poptReadDefaultConfig(pc
, 0);
341 /* Default event type */
342 opt_event_type
= LTTNG_EVENT_ALL
;
344 while ((opt
= poptGetNextOpt(pc
)) != -1) {
353 opt_event_type
= LTTNG_EVENT_SYSCALL
;
355 case OPT_LIST_OPTIONS
:
356 list_cmd_options(stdout
, long_options
);
364 /* Validate event type. Multiple event type are not supported. */
365 if (event_type
== -1) {
366 event_type
= opt_event_type
;
368 if (event_type
!= opt_event_type
) {
369 ERR("Multiple event type not supported.");
376 opt_event_list
= (char*) poptGetArg(pc
);
377 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
378 ERR("Missing event name(s).\n");
384 if (!opt_session_name
) {
385 session_name
= get_session_name();
386 if (session_name
== NULL
) {
391 session_name
= opt_session_name
;
396 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
398 ret
= -LTTNG_ERR_NOMEM
;
402 /* Open command element */
403 ret
= mi_lttng_writer_command_open(writer
,
404 mi_lttng_element_command_disable_event
);
410 /* Open output element */
411 ret
= mi_lttng_writer_open_element(writer
,
412 mi_lttng_element_command_output
);
419 command_ret
= disable_events(session_name
);
426 /* Close output element */
427 ret
= mi_lttng_writer_close_element(writer
);
433 ret
= mi_lttng_writer_write_element_bool(writer
,
434 mi_lttng_element_command_success
, success
);
440 /* Command element close */
441 ret
= mi_lttng_writer_command_close(writer
);
449 if (!opt_session_name
&& session_name
) {
454 if (writer
&& mi_lttng_writer_destroy(writer
)) {
455 /* Preserve original error code */
456 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
459 /* Overwrite ret if an error occured in disable_events */
460 ret
= command_ret
? command_ret
: ret
;