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 #ifdef LTTNG_EMBED_HELP
44 static const char help_msg
[] =
45 #include <lttng-disable-event.1.h>
59 static struct lttng_handle
*handle
;
60 static struct mi_writer
*writer
;
62 static struct poptOption long_options
[] = {
63 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
64 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
65 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
66 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
67 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
68 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
69 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
70 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
71 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
72 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
73 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_TYPE_SYSCALL
, 0, 0},
74 {"probe", 0, POPT_ARG_NONE
, 0, OPT_TYPE_PROBE
, 0, 0},
75 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TYPE_TRACEPOINT
, 0, 0},
76 {"function", 0, POPT_ARG_NONE
, 0, OPT_TYPE_FUNCTION
, 0, 0},
77 {"all", 0, POPT_ARG_NONE
, 0, OPT_TYPE_ALL
, 0, 0},
78 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
83 const char *print_channel_name(const char *name
)
85 return name
? : DEFAULT_CHANNEL_NAME
;
89 const char *print_raw_channel_name(const char *name
)
91 return name
? : "<default>";
95 const char *print_event_type(const enum lttng_event_type ev_type
)
100 case LTTNG_EVENT_TRACEPOINT
:
102 case LTTNG_EVENT_PROBE
:
104 case LTTNG_EVENT_FUNCTION
:
106 case LTTNG_EVENT_FUNCTION_ENTRY
:
107 return "function entry";
108 case LTTNG_EVENT_SYSCALL
:
115 /* Mi print a partial event.
119 static int mi_print_event(char *event_name
, int enabled
, int success
)
126 /* Open event element */
127 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
132 /* Print the name of event */
133 ret
= mi_lttng_writer_write_element_string(writer
,
134 config_element_name
, event_name
);
139 /* Print enabled ? */
140 ret
= mi_lttng_writer_write_element_bool(writer
,
141 config_element_enabled
, enabled
);
147 ret
= mi_lttng_writer_write_element_bool(writer
,
148 mi_lttng_element_command_success
, success
);
153 /* Close event element */
154 ret
= mi_lttng_writer_close_element(writer
);
162 * Disabling event using the lttng API.
164 static int disable_events(char *session_name
)
166 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
167 int enabled
= 1, success
= 1;
168 char *event_name
, *channel_name
= NULL
;
169 struct lttng_domain dom
;
170 struct lttng_event event
;
172 memset(&dom
, 0, sizeof(dom
));
174 /* Create lttng domain */
176 dom
.type
= LTTNG_DOMAIN_KERNEL
;
177 } else if (opt_userspace
) {
178 dom
.type
= LTTNG_DOMAIN_UST
;
179 } else if (opt_jul
) {
180 dom
.type
= LTTNG_DOMAIN_JUL
;
181 } else if (opt_log4j
) {
182 dom
.type
= LTTNG_DOMAIN_LOG4J
;
183 } else if (opt_python
) {
184 dom
.type
= LTTNG_DOMAIN_PYTHON
;
186 /* Checked by the caller. */
190 channel_name
= opt_channel_name
;
192 handle
= lttng_create_handle(session_name
, &dom
);
193 if (handle
== NULL
) {
198 /* Mi print the channel and open the events element */
200 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
206 ret
= mi_lttng_writer_write_element_string(writer
,
207 config_element_name
, print_channel_name(channel_name
));
213 /* Open events element */
214 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
221 memset(&event
, 0, sizeof(event
));
222 /* Set default loglevel to any/unknown */
225 /* opt_event_type contain the event type to disable at this point */
226 event
.type
= opt_event_type
;
228 if (opt_disable_all
) {
229 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
230 if (command_ret
< 0) {
231 ERR("%s", lttng_strerror(command_ret
));
238 MSG("All %s events of type %s are disabled in channel %s",
239 get_domain_str(dom
.type
),
240 print_event_type(opt_event_type
),
241 print_channel_name(channel_name
));
245 ret
= mi_print_event("*", enabled
, success
);
252 /* Strip event list */
253 event_name
= strtok(opt_event_list
, ",");
254 while (event_name
!= NULL
) {
255 DBG("Disabling event %s", event_name
);
257 strncpy(event
.name
, event_name
, sizeof(event
.name
));
258 event
.name
[sizeof(event
.name
) - 1] = '\0';
259 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
260 if (command_ret
< 0) {
261 ERR("%s of type %s : %s (channel %s, session %s)",
263 print_event_type(opt_event_type
),
264 lttng_strerror(command_ret
),
265 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
266 ? print_raw_channel_name(channel_name
)
267 : print_channel_name(channel_name
),
272 * If an error occurred we assume that the event is still
277 MSG("%s %s of type %s disabled in channel %s for session %s",
278 get_domain_str(dom
.type
),
280 print_event_type(opt_event_type
),
281 print_channel_name(channel_name
),
288 ret
= mi_print_event(event_name
, enabled
, success
);
296 event_name
= strtok(NULL
, ",");
302 /* Close events element and channel element */
303 ret
= mi_lttng_close_multi_element(writer
, 2);
309 /* if there is already an error preserve it */
314 /* Overwrite ret if an error occurred */
315 ret
= command_ret
? command_ret
: ret
;
317 lttng_destroy_handle(handle
);
324 * Disable event to trace session
326 int cmd_disable_events(int argc
, const char **argv
)
328 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
329 static poptContext pc
;
330 char *session_name
= NULL
;
331 const char *leftover
= NULL
;
334 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
335 poptReadDefaultConfig(pc
, 0);
337 /* Default event type */
338 opt_event_type
= LTTNG_EVENT_ALL
;
340 while ((opt
= poptGetNextOpt(pc
)) != -1) {
345 case OPT_TYPE_SYSCALL
:
346 opt_event_type
= LTTNG_EVENT_SYSCALL
;
348 case OPT_TYPE_TRACEPOINT
:
349 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
352 opt_event_type
= LTTNG_EVENT_PROBE
;
354 case OPT_TYPE_FUNCTION
:
355 opt_event_type
= LTTNG_EVENT_FUNCTION
;
358 opt_event_type
= LTTNG_EVENT_ALL
;
360 case OPT_LIST_OPTIONS
:
361 list_cmd_options(stdout
, long_options
);
368 /* Validate event type. Multiple event type are not supported. */
369 if (event_type
== -1) {
370 event_type
= opt_event_type
;
372 if (event_type
!= opt_event_type
) {
373 ERR("Multiple event type not supported.");
380 ret
= print_missing_or_multiple_domains(
381 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
);
387 /* Ust and agent only support ALL event type */
388 if ((opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
)
389 && opt_event_type
!= LTTNG_EVENT_ALL
) {
390 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
395 opt_event_list
= (char*) poptGetArg(pc
);
396 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
397 ERR("Missing event name(s).\n");
402 leftover
= poptGetArg(pc
);
404 ERR("Unknown argument: %s", leftover
);
409 if (!opt_session_name
) {
410 session_name
= get_session_name();
411 if (session_name
== NULL
) {
416 session_name
= opt_session_name
;
421 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
423 ret
= -LTTNG_ERR_NOMEM
;
427 /* Open command element */
428 ret
= mi_lttng_writer_command_open(writer
,
429 mi_lttng_element_command_disable_event
);
435 /* Open output element */
436 ret
= mi_lttng_writer_open_element(writer
,
437 mi_lttng_element_command_output
);
444 command_ret
= disable_events(session_name
);
451 /* Close output element */
452 ret
= mi_lttng_writer_close_element(writer
);
458 ret
= mi_lttng_writer_write_element_bool(writer
,
459 mi_lttng_element_command_success
, success
);
465 /* Command element close */
466 ret
= mi_lttng_writer_command_close(writer
);
474 if (!opt_session_name
&& session_name
) {
479 if (writer
&& mi_lttng_writer_destroy(writer
)) {
480 /* Preserve original error code */
481 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
484 /* Overwrite ret if an error occurred in disable_events */
485 ret
= command_ret
? command_ret
: ret
;