2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
13 #include <lttng/domain-internal.hpp>
20 #include <sys/types.h>
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
, nullptr, OPT_HELP
, nullptr, nullptr },
55 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
56 { "all-events", 'a', POPT_ARG_VAL
, &opt_disable_all
, 1, nullptr, nullptr },
57 { "channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, nullptr, nullptr },
58 { "jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, nullptr, nullptr },
59 { "log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, nullptr, nullptr },
60 { "python", 'p', POPT_ARG_VAL
, &opt_python
, 1, nullptr, nullptr },
61 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
62 { "userspace", 'u', POPT_ARG_VAL
, &opt_userspace
, 1, nullptr, nullptr },
63 { "syscall", 0, POPT_ARG_NONE
, nullptr, OPT_TYPE_SYSCALL
, nullptr, nullptr },
64 { "probe", 0, POPT_ARG_NONE
, nullptr, OPT_TYPE_PROBE
, nullptr, nullptr },
65 { "tracepoint", 0, POPT_ARG_NONE
, nullptr, OPT_TYPE_TRACEPOINT
, nullptr, nullptr },
66 { "function", 0, POPT_ARG_NONE
, nullptr, OPT_TYPE_FUNCTION
, nullptr, nullptr },
67 { "all", 0, POPT_ARG_NONE
, nullptr, OPT_TYPE_ALL
, nullptr, nullptr },
68 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
69 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
72 static const char *print_channel_name(const char *name
)
74 return name
?: DEFAULT_CHANNEL_NAME
;
77 static const char *print_raw_channel_name(const char *name
)
79 return name
?: "<default>";
82 static const char *print_event_type(const enum lttng_event_type ev_type
)
87 case LTTNG_EVENT_TRACEPOINT
:
89 case LTTNG_EVENT_PROBE
:
91 case LTTNG_EVENT_FUNCTION
:
93 case LTTNG_EVENT_FUNCTION_ENTRY
:
94 return "function entry";
95 case LTTNG_EVENT_SYSCALL
:
102 /* Mi print a partial event.
106 static int mi_print_event(const char *event_name
, int enabled
, int success
)
110 LTTNG_ASSERT(writer
);
111 LTTNG_ASSERT(event_name
);
113 /* Open event element */
114 ret
= mi_lttng_writer_open_element(writer
, config_element_event
);
119 /* Print the name of event */
120 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, event_name
);
125 /* Print enabled ? */
126 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
, enabled
);
132 ret
= mi_lttng_writer_write_element_bool(writer
, mi_lttng_element_command_success
, success
);
137 /* Close event element */
138 ret
= mi_lttng_writer_close_element(writer
);
146 * Disabling event using the lttng API.
148 static int disable_events(char *session_name
, char *event_list
)
150 enum cmd_error_code ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
151 bool enabled
= true, success
= true, warn
= false;
152 char *event_name
, *channel_name
= nullptr;
153 struct lttng_domain dom
;
154 struct lttng_event event
;
156 memset(&dom
, 0, sizeof(dom
));
158 /* Create lttng domain */
160 dom
.type
= LTTNG_DOMAIN_KERNEL
;
161 } else if (opt_userspace
) {
162 dom
.type
= LTTNG_DOMAIN_UST
;
163 } else if (opt_jul
) {
164 dom
.type
= LTTNG_DOMAIN_JUL
;
165 } else if (opt_log4j
) {
166 dom
.type
= LTTNG_DOMAIN_LOG4J
;
167 } else if (opt_python
) {
168 dom
.type
= LTTNG_DOMAIN_PYTHON
;
170 /* Checked by the caller. */
174 channel_name
= opt_channel_name
;
176 handle
= lttng_create_handle(session_name
, &dom
);
177 if (handle
== nullptr) {
182 /* Mi print the channel and open the events element */
184 int mi_ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
190 mi_ret
= mi_lttng_writer_write_element_string(
191 writer
, config_element_name
, print_channel_name(channel_name
));
197 /* Open events element */
198 mi_ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
205 memset(&event
, 0, sizeof(event
));
206 /* Set default loglevel to any/unknown */
209 /* opt_event_type contain the event type to disable at this point */
210 event
.type
= (lttng_event_type
) opt_event_type
;
212 if (opt_disable_all
) {
213 const int disable_ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, nullptr);
215 if (disable_ret
< 0) {
216 ERR("%s", lttng_strerror(command_ret
));
217 command_ret
= CMD_ERROR
;
223 MSG("All %s events of type %s are disabled in channel %s",
224 lttng_domain_type_str(dom
.type
),
225 print_event_type((lttng_event_type
) opt_event_type
),
226 print_channel_name(channel_name
));
230 const int mi_ret
= mi_print_event("*", enabled
, success
);
238 /* Strip event list */
239 event_name
= strtok(event_list
, ",");
240 while (event_name
!= nullptr) {
241 DBG("Disabling event %s", event_name
);
243 strncpy(event
.name
, event_name
, sizeof(event
.name
));
244 event
.name
[sizeof(event
.name
) - 1] = '\0';
245 const int disable_ret
=
246 lttng_disable_event_ext(handle
, &event
, channel_name
, nullptr);
247 if (disable_ret
< 0) {
248 ERR("%s of type %s : %s (channel %s, session %s)",
250 print_event_type((lttng_event_type
) opt_event_type
),
251 lttng_strerror(disable_ret
),
252 disable_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
?
253 print_raw_channel_name(channel_name
) :
254 print_channel_name(channel_name
),
259 * If an error occurred we assume that the event is still
263 command_ret
= CMD_ERROR
;
265 MSG("%s %s of type %s disabled in channel %s for session %s",
266 lttng_domain_type_str(dom
.type
),
268 print_event_type((lttng_event_type
) opt_event_type
),
269 print_channel_name(channel_name
),
276 const int mi_ret
= mi_print_event(event_name
, enabled
, success
);
285 event_name
= strtok(nullptr, ",");
291 /* Close events element and channel element */
292 const int mi_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
= nullptr;
321 char *event_list
= nullptr;
322 const char *arg_event_list
= nullptr;
323 const char *leftover
= nullptr;
326 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
327 poptReadDefaultConfig(pc
, 0);
329 /* Default event type */
330 opt_event_type
= LTTNG_EVENT_ALL
;
332 while ((opt
= poptGetNextOpt(pc
)) != -1) {
337 case OPT_TYPE_SYSCALL
:
338 opt_event_type
= LTTNG_EVENT_SYSCALL
;
340 case OPT_TYPE_TRACEPOINT
:
341 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
344 opt_event_type
= LTTNG_EVENT_PROBE
;
346 case OPT_TYPE_FUNCTION
:
347 opt_event_type
= LTTNG_EVENT_FUNCTION
;
350 opt_event_type
= LTTNG_EVENT_ALL
;
352 case OPT_LIST_OPTIONS
:
353 list_cmd_options(stdout
, long_options
);
360 /* Validate event type. Multiple event type are not supported. */
361 if (event_type
== -1) {
362 event_type
= opt_event_type
;
364 if (event_type
!= opt_event_type
) {
365 ERR("Multiple event type not supported.");
372 ret
= print_missing_or_multiple_domains(
373 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
+ opt_python
, true);
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 arg_event_list
= poptGetArg(pc
);
388 if (arg_event_list
== nullptr && opt_disable_all
== 0) {
389 ERR("Missing event name(s).\n");
394 if (opt_disable_all
== 0) {
395 event_list
= strdup(arg_event_list
);
396 if (event_list
== nullptr) {
397 PERROR("Failed to copy event name(s)");
403 leftover
= poptGetArg(pc
);
405 ERR("Unknown argument: %s", leftover
);
410 if (!opt_session_name
) {
411 session_name
= get_session_name();
412 if (session_name
== nullptr) {
417 session_name
= opt_session_name
;
422 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
424 ret
= -LTTNG_ERR_NOMEM
;
428 /* Open command element */
429 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_disable_event
);
435 /* Open output element */
436 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
443 command_ret
= disable_events(session_name
, event_list
);
450 /* Close output element */
451 ret
= mi_lttng_writer_close_element(writer
);
457 ret
= mi_lttng_writer_write_element_bool(
458 writer
, mi_lttng_element_command_success
, success
);
464 /* Command element close */
465 ret
= mi_lttng_writer_command_close(writer
);
473 if (!opt_session_name
&& session_name
) {
480 if (writer
&& mi_lttng_writer_destroy(writer
)) {
481 /* Preserve original error code */
482 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
485 /* Overwrite ret if an error occurred in disable_events */
486 ret
= command_ret
? command_ret
: ret
;