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 /* Not implemented yet */
41 static char *opt_cmd_name
;
51 static struct lttng_handle
*handle
;
52 static struct mi_writer
*writer
;
54 static struct poptOption long_options
[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
57 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
58 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
59 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
60 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
61 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
63 /* Not implemented yet */
64 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
65 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
67 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
69 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
76 static void usage(FILE *ofp
)
78 fprintf(ofp
, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
80 fprintf(ofp
, "Options:\n");
81 fprintf(ofp
, " -h, --help Show this help\n");
82 fprintf(ofp
, " --list-options Simple listing of options\n");
83 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
84 fprintf(ofp
, " -c, --channel NAME Apply to this channel\n");
85 fprintf(ofp
, " -a, --all-events Disable all tracepoints\n");
86 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
87 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
88 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
93 const char *print_channel_name(const char *name
)
95 return name
? : DEFAULT_CHANNEL_NAME
;
99 const char *print_raw_channel_name(const char *name
)
101 return name
? : "<default>";
104 /* Mi print a partial event.
108 static int mi_print_event(char *event_name
, int enabled
, int success
)
115 /* Open event element */
116 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
121 /* Print the name of event */
122 ret
= mi_lttng_writer_write_element_string(writer
,
123 config_element_name
, event_name
);
128 /* Print enabled ? */
129 ret
= mi_lttng_writer_write_element_bool(writer
,
130 config_element_enabled
, enabled
);
136 ret
= mi_lttng_writer_write_element_bool(writer
,
137 mi_lttng_element_command_success
, success
);
142 /* Close event element */
143 ret
= mi_lttng_writer_close_element(writer
);
151 * Disabling event using the lttng API.
153 static int disable_events(char *session_name
)
155 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
156 int enabled
= 1, success
= 1;
157 char *event_name
, *channel_name
= NULL
;
158 struct lttng_domain dom
;
160 memset(&dom
, 0, sizeof(dom
));
162 /* Create lttng domain */
164 dom
.type
= LTTNG_DOMAIN_KERNEL
;
165 } else if (opt_userspace
) {
166 dom
.type
= LTTNG_DOMAIN_UST
;
167 } else if (opt_jul
) {
168 dom
.type
= LTTNG_DOMAIN_JUL
;
170 print_missing_domain();
175 channel_name
= opt_channel_name
;
177 handle
= lttng_create_handle(session_name
, &dom
);
178 if (handle
== NULL
) {
183 /* Mi print the channel and open the events element */
185 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
191 ret
= mi_lttng_writer_write_element_string(writer
,
192 config_element_name
, print_channel_name(channel_name
));
198 /* Open events element */
199 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
206 if (opt_disable_all
) {
207 command_ret
= lttng_disable_event(handle
, NULL
, channel_name
);
208 if (command_ret
< 0) {
209 ERR("%s", lttng_strerror(command_ret
));
216 MSG("All %s events are disabled in channel %s",
217 get_domain_str(dom
.type
), print_channel_name(channel_name
));
221 ret
= mi_print_event("*", enabled
, success
);
228 /* Strip event list */
229 event_name
= strtok(opt_event_list
, ",");
230 while (event_name
!= NULL
) {
231 DBG("Disabling event %s", event_name
);
233 command_ret
= lttng_disable_event(handle
, event_name
, channel_name
);
234 if (command_ret
< 0) {
235 ERR("Event %s: %s (channel %s, session %s)", event_name
,
236 lttng_strerror(command_ret
),
237 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
238 ? print_raw_channel_name(channel_name
)
239 : print_channel_name(channel_name
),
244 * If an error occurred we assume that
245 * the event is still enabled.
249 MSG("%s event %s disabled in channel %s for session %s",
250 get_domain_str(dom
.type
), event_name
,
251 print_channel_name(channel_name
),
258 ret
= mi_print_event(event_name
, enabled
, success
);
266 event_name
= strtok(NULL
, ",");
272 /* Close events element and channel element */
273 ret
= mi_lttng_close_multi_element(writer
, 2);
279 /* if there is already an error preserve it */
284 /* Overwrite ret if an error occurred */
285 ret
= command_ret
? command_ret
: ret
;
287 lttng_destroy_handle(handle
);
294 * Disable event to trace session
296 int cmd_disable_events(int argc
, const char **argv
)
298 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
299 static poptContext pc
;
300 char *session_name
= NULL
;
302 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
303 poptReadDefaultConfig(pc
, 0);
305 while ((opt
= poptGetNextOpt(pc
)) != -1) {
313 case OPT_LIST_OPTIONS
:
314 list_cmd_options(stdout
, long_options
);
323 opt_event_list
= (char*) poptGetArg(pc
);
324 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
325 ERR("Missing event name(s).\n");
331 if (!opt_session_name
) {
332 session_name
= get_session_name();
333 if (session_name
== NULL
) {
338 session_name
= opt_session_name
;
343 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
345 ret
= -LTTNG_ERR_NOMEM
;
349 /* Open command element */
350 ret
= mi_lttng_writer_command_open(writer
,
351 mi_lttng_element_command_disable_event
);
357 /* Open output element */
358 ret
= mi_lttng_writer_open_element(writer
,
359 mi_lttng_element_command_output
);
366 command_ret
= disable_events(session_name
);
373 /* Close output element */
374 ret
= mi_lttng_writer_close_element(writer
);
380 ret
= mi_lttng_writer_write_element_bool(writer
,
381 mi_lttng_element_command_success
, success
);
387 /* Command element close */
388 ret
= mi_lttng_writer_command_close(writer
);
396 if (!opt_session_name
&& session_name
) {
401 if (writer
&& mi_lttng_writer_destroy(writer
)) {
402 /* Preserve original error code */
403 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
406 /* Overwrite ret if an error occured in disable_events */
407 ret
= command_ret
? command_ret
: ret
;