2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
22 static char *opt_event_list
;
23 static int opt_kernel
;
24 static char *opt_channel_name
;
25 static char *opt_session_name
;
26 static int opt_userspace
;
27 static int opt_disable_all
;
30 static int opt_python
;
31 static int opt_event_type
;
33 #ifdef LTTNG_EMBED_HELP
34 static const char help_msg
[] =
35 #include <lttng-disable-event.1.h>
49 static struct lttng_handle
*handle
;
50 static struct mi_writer
*writer
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
56 {"all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, 0, 0},
57 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
58 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
59 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
60 {"python", 'p', POPT_ARG_VAL
, &opt_python
, 1, 0, 0},
61 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
62 {"userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, 0, 0},
63 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_TYPE_SYSCALL
, 0, 0},
64 {"probe", 0, POPT_ARG_NONE
, 0, OPT_TYPE_PROBE
, 0, 0},
65 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TYPE_TRACEPOINT
, 0, 0},
66 {"function", 0, POPT_ARG_NONE
, 0, OPT_TYPE_FUNCTION
, 0, 0},
67 {"all", 0, POPT_ARG_NONE
, 0, OPT_TYPE_ALL
, 0, 0},
68 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
73 const char *print_channel_name(const char *name
)
75 return name
? : DEFAULT_CHANNEL_NAME
;
79 const char *print_raw_channel_name(const char *name
)
81 return name
? : "<default>";
85 const char *print_event_type(const enum lttng_event_type ev_type
)
90 case LTTNG_EVENT_TRACEPOINT
:
92 case LTTNG_EVENT_PROBE
:
94 case LTTNG_EVENT_FUNCTION
:
96 case LTTNG_EVENT_FUNCTION_ENTRY
:
97 return "function entry";
98 case LTTNG_EVENT_SYSCALL
:
105 /* Mi print a partial event.
109 static int mi_print_event(const char *event_name
, int enabled
, int success
)
116 /* Open event element */
117 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
122 /* Print the name of event */
123 ret
= mi_lttng_writer_write_element_string(writer
,
124 config_element_name
, event_name
);
129 /* Print enabled ? */
130 ret
= mi_lttng_writer_write_element_bool(writer
,
131 config_element_enabled
, enabled
);
137 ret
= mi_lttng_writer_write_element_bool(writer
,
138 mi_lttng_element_command_success
, success
);
143 /* Close event element */
144 ret
= mi_lttng_writer_close_element(writer
);
152 * Disabling event using the lttng API.
154 static int disable_events(char *session_name
)
156 int ret
= CMD_SUCCESS
, warn
= 0, command_ret
= CMD_SUCCESS
;
157 int enabled
= 1, success
= 1;
158 char *event_name
, *channel_name
= NULL
;
159 struct lttng_domain dom
;
160 struct lttng_event event
;
162 memset(&dom
, 0, sizeof(dom
));
164 /* Create lttng domain */
166 dom
.type
= LTTNG_DOMAIN_KERNEL
;
167 } else if (opt_userspace
) {
168 dom
.type
= LTTNG_DOMAIN_UST
;
169 } else if (opt_jul
) {
170 dom
.type
= LTTNG_DOMAIN_JUL
;
171 } else if (opt_log4j
) {
172 dom
.type
= LTTNG_DOMAIN_LOG4J
;
173 } else if (opt_python
) {
174 dom
.type
= LTTNG_DOMAIN_PYTHON
;
176 /* Checked by the caller. */
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 memset(&event
, 0, sizeof(event
));
212 /* Set default loglevel to any/unknown */
215 /* opt_event_type contain the event type to disable at this point */
216 event
.type
= opt_event_type
;
218 if (opt_disable_all
) {
219 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
220 if (command_ret
< 0) {
221 ERR("%s", lttng_strerror(command_ret
));
228 MSG("All %s events of type %s are disabled in channel %s",
229 get_domain_str(dom
.type
),
230 print_event_type(opt_event_type
),
231 print_channel_name(channel_name
));
235 ret
= mi_print_event("*", enabled
, success
);
242 /* Strip event list */
243 event_name
= strtok(opt_event_list
, ",");
244 while (event_name
!= NULL
) {
245 DBG("Disabling event %s", event_name
);
247 strncpy(event
.name
, event_name
, sizeof(event
.name
));
248 event
.name
[sizeof(event
.name
) - 1] = '\0';
249 command_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
250 if (command_ret
< 0) {
251 ERR("%s of type %s : %s (channel %s, session %s)",
253 print_event_type(opt_event_type
),
254 lttng_strerror(command_ret
),
255 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
256 ? print_raw_channel_name(channel_name
)
257 : print_channel_name(channel_name
),
262 * If an error occurred we assume that the event is still
267 MSG("%s %s of type %s disabled in channel %s for session %s",
268 get_domain_str(dom
.type
),
270 print_event_type(opt_event_type
),
271 print_channel_name(channel_name
),
278 ret
= mi_print_event(event_name
, enabled
, success
);
286 event_name
= strtok(NULL
, ",");
292 /* Close events element and channel element */
293 ret
= mi_lttng_close_multi_element(writer
, 2);
299 /* if there is already an error preserve it */
304 /* Overwrite ret if an error occurred */
305 ret
= command_ret
? command_ret
: ret
;
307 lttng_destroy_handle(handle
);
314 * Disable event to trace session
316 int cmd_disable_events(int argc
, const char **argv
)
318 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
319 static poptContext pc
;
320 char *session_name
= NULL
;
321 const char *leftover
= NULL
;
324 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
325 poptReadDefaultConfig(pc
, 0);
327 /* Default event type */
328 opt_event_type
= LTTNG_EVENT_ALL
;
330 while ((opt
= poptGetNextOpt(pc
)) != -1) {
335 case OPT_TYPE_SYSCALL
:
336 opt_event_type
= LTTNG_EVENT_SYSCALL
;
338 case OPT_TYPE_TRACEPOINT
:
339 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
342 opt_event_type
= LTTNG_EVENT_PROBE
;
344 case OPT_TYPE_FUNCTION
:
345 opt_event_type
= LTTNG_EVENT_FUNCTION
;
348 opt_event_type
= LTTNG_EVENT_ALL
;
350 case OPT_LIST_OPTIONS
:
351 list_cmd_options(stdout
, long_options
);
358 /* Validate event type. Multiple event type are not supported. */
359 if (event_type
== -1) {
360 event_type
= opt_event_type
;
362 if (event_type
!= opt_event_type
) {
363 ERR("Multiple event type not supported.");
370 ret
= print_missing_or_multiple_domains(
371 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+
379 /* Ust and agent only support ALL event type */
380 if ((opt_userspace
|| opt_jul
|| opt_log4j
|| opt_python
)
381 && opt_event_type
!= LTTNG_EVENT_ALL
) {
382 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
387 opt_event_list
= (char*) poptGetArg(pc
);
388 if (opt_event_list
== NULL
&& opt_disable_all
== 0) {
389 ERR("Missing event name(s).\n");
394 leftover
= poptGetArg(pc
);
396 ERR("Unknown argument: %s", leftover
);
401 if (!opt_session_name
) {
402 session_name
= get_session_name();
403 if (session_name
== NULL
) {
408 session_name
= opt_session_name
;
413 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
415 ret
= -LTTNG_ERR_NOMEM
;
419 /* Open command element */
420 ret
= mi_lttng_writer_command_open(writer
,
421 mi_lttng_element_command_disable_event
);
427 /* Open output element */
428 ret
= mi_lttng_writer_open_element(writer
,
429 mi_lttng_element_command_output
);
436 command_ret
= disable_events(session_name
);
443 /* Close output element */
444 ret
= mi_lttng_writer_close_element(writer
);
450 ret
= mi_lttng_writer_write_element_bool(writer
,
451 mi_lttng_element_command_success
, success
);
457 /* Command element close */
458 ret
= mi_lttng_writer_command_close(writer
);
466 if (!opt_session_name
&& session_name
) {
471 if (writer
&& mi_lttng_writer_destroy(writer
)) {
472 /* Preserve original error code */
473 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
476 /* Overwrite ret if an error occurred in disable_events */
477 ret
= command_ret
? command_ret
: ret
;