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
;
41 /* Not implemented yet */
42 static char *opt_cmd_name
;
52 static struct lttng_handle
*handle
;
53 static struct mi_writer
*writer
;
55 static struct poptOption long_options
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
58 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
59 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
60 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
61 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
62 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
63 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
65 /* Not implemented yet */
66 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
67 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
69 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
71 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
78 static void usage(FILE *ofp
)
80 fprintf(ofp
, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
82 fprintf(ofp
, "Options:\n");
83 fprintf(ofp
, " -h, --help Show this help\n");
84 fprintf(ofp
, " --list-options Simple listing of options\n");
85 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
86 fprintf(ofp
, " -c, --channel NAME Apply to this channel\n");
87 fprintf(ofp
, " -a, --all-events Disable all tracepoints\n");
88 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
89 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
90 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
91 fprintf(ofp
, " -l, --log4j Apply to Java application using LOG4j\n");
96 const char *print_channel_name(const char *name
)
98 return name
? : DEFAULT_CHANNEL_NAME
;
102 const char *print_raw_channel_name(const char *name
)
104 return name
? : "<default>";
107 /* Mi print a partial event.
111 static int mi_print_event(char *event_name
, int enabled
, int success
)
118 /* Open event element */
119 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
124 /* Print the name of event */
125 ret
= mi_lttng_writer_write_element_string(writer
,
126 config_element_name
, event_name
);
131 /* Print enabled ? */
132 ret
= mi_lttng_writer_write_element_bool(writer
,
133 config_element_enabled
, enabled
);
139 ret
= mi_lttng_writer_write_element_bool(writer
,
140 mi_lttng_element_command_success
, success
);
145 /* Close event element */
146 ret
= mi_lttng_writer_close_element(writer
);
154 * Disabling event using the lttng API.
156 static int disable_events(char *session_name
)
158 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
159 int enabled
= 1, success
= 1;
160 char *event_name
, *channel_name
= NULL
;
161 struct lttng_domain dom
;
163 memset(&dom
, 0, sizeof(dom
));
165 /* Create lttng domain */
167 dom
.type
= LTTNG_DOMAIN_KERNEL
;
168 } else if (opt_userspace
) {
169 dom
.type
= LTTNG_DOMAIN_UST
;
170 } else if (opt_jul
) {
171 dom
.type
= LTTNG_DOMAIN_JUL
;
172 } else if (opt_log4j
) {
173 dom
.type
= LTTNG_DOMAIN_LOG4J
;
175 print_missing_domain();
180 channel_name
= opt_channel_name
;
182 handle
= lttng_create_handle(session_name
, &dom
);
183 if (handle
== NULL
) {
188 /* Mi print the channel and open the events element */
190 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
196 ret
= mi_lttng_writer_write_element_string(writer
,
197 config_element_name
, print_channel_name(channel_name
));
203 /* Open events element */
204 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
211 if (opt_disable_all
) {
212 command_ret
= lttng_disable_event(handle
, NULL
, channel_name
);
213 if (command_ret
< 0) {
214 ERR("%s", lttng_strerror(command_ret
));
221 MSG("All %s events are disabled in channel %s",
222 get_domain_str(dom
.type
), print_channel_name(channel_name
));
226 ret
= mi_print_event("*", enabled
, success
);
233 /* Strip event list */
234 event_name
= strtok(opt_event_list
, ",");
235 while (event_name
!= NULL
) {
236 DBG("Disabling event %s", event_name
);
238 command_ret
= lttng_disable_event(handle
, event_name
, channel_name
);
239 if (command_ret
< 0) {
240 ERR("Event %s: %s (channel %s, session %s)", event_name
,
241 lttng_strerror(command_ret
),
242 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
243 ? print_raw_channel_name(channel_name
)
244 : print_channel_name(channel_name
),
249 * If an error occurred we assume that
250 * the event is still enabled.
254 MSG("%s event %s disabled in channel %s for session %s",
255 get_domain_str(dom
.type
), event_name
,
256 print_channel_name(channel_name
),
263 ret
= mi_print_event(event_name
, enabled
, success
);
271 event_name
= strtok(NULL
, ",");
277 /* Close events element and channel element */
278 ret
= mi_lttng_close_multi_element(writer
, 2);
284 /* if there is already an error preserve it */
289 /* Overwrite ret if an error occurred */
290 ret
= command_ret
? command_ret
: ret
;
292 lttng_destroy_handle(handle
);
299 * Disable event to trace session
301 int cmd_disable_events(int argc
, const char **argv
)
303 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
304 static poptContext pc
;
305 char *session_name
= NULL
;
307 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
308 poptReadDefaultConfig(pc
, 0);
310 while ((opt
= poptGetNextOpt(pc
)) != -1) {
318 case OPT_LIST_OPTIONS
:
319 list_cmd_options(stdout
, long_options
);
328 opt_event_list
= (char*) poptGetArg(pc
);
329 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
330 ERR("Missing event name(s).\n");
336 if (!opt_session_name
) {
337 session_name
= get_session_name();
338 if (session_name
== NULL
) {
343 session_name
= opt_session_name
;
348 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
350 ret
= -LTTNG_ERR_NOMEM
;
354 /* Open command element */
355 ret
= mi_lttng_writer_command_open(writer
,
356 mi_lttng_element_command_disable_event
);
362 /* Open output element */
363 ret
= mi_lttng_writer_open_element(writer
,
364 mi_lttng_element_command_output
);
371 command_ret
= disable_events(session_name
);
378 /* Close output element */
379 ret
= mi_lttng_writer_close_element(writer
);
385 ret
= mi_lttng_writer_write_element_bool(writer
,
386 mi_lttng_element_command_success
, success
);
392 /* Command element close */
393 ret
= mi_lttng_writer_command_close(writer
);
401 if (!opt_session_name
&& session_name
) {
406 if (writer
&& mi_lttng_writer_destroy(writer
)) {
407 /* Preserve original error code */
408 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
411 /* Overwrite ret if an error occured in disable_events */
412 ret
= command_ret
? command_ret
: ret
;