Commit | Line | Data |
---|---|---|
e953ef25 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
e953ef25 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
e953ef25 | 5 | * |
e953ef25 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | ||
13 | #include <lttng/domain-internal.hpp> | |
14 | ||
e953ef25 DG |
15 | #include <popt.h> |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <sys/stat.h> | |
20 | #include <sys/types.h> | |
21 | #include <unistd.h> | |
e4d484a5 | 22 | |
9730260e | 23 | static int opt_kernel; |
e953ef25 | 24 | static char *opt_channel_name; |
5440dc42 | 25 | static char *opt_session_name; |
e953ef25 DG |
26 | static int opt_userspace; |
27 | static int opt_disable_all; | |
b9dfb167 | 28 | static int opt_jul; |
5cdb6027 | 29 | static int opt_log4j; |
0e115563 | 30 | static int opt_python; |
6e911cad | 31 | static int opt_event_type; |
e953ef25 | 32 | |
4fc83d94 PP |
33 | #ifdef LTTNG_EMBED_HELP |
34 | static const char help_msg[] = | |
35 | #include <lttng-disable-event.1.h> | |
28ab034a | 36 | ; |
4fc83d94 PP |
37 | #endif |
38 | ||
e953ef25 DG |
39 | enum { |
40 | OPT_HELP = 1, | |
9550ee81 JR |
41 | OPT_TYPE_SYSCALL, |
42 | OPT_TYPE_TRACEPOINT, | |
43 | OPT_TYPE_PROBE, | |
44 | OPT_TYPE_FUNCTION, | |
45 | OPT_TYPE_ALL, | |
679b4943 | 46 | OPT_LIST_OPTIONS, |
e953ef25 DG |
47 | }; |
48 | ||
cd80958d | 49 | static struct lttng_handle *handle; |
e4d484a5 | 50 | static struct mi_writer *writer; |
cd80958d | 51 | |
e953ef25 DG |
52 | static struct poptOption long_options[] = { |
53 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
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 } | |
e953ef25 DG |
70 | }; |
71 | ||
28ab034a | 72 | static const char *print_channel_name(const char *name) |
85076754 | 73 | { |
28ab034a | 74 | return name ?: DEFAULT_CHANNEL_NAME; |
85076754 MD |
75 | } |
76 | ||
28ab034a | 77 | static const char *print_raw_channel_name(const char *name) |
85076754 | 78 | { |
28ab034a | 79 | return name ?: "<default>"; |
85076754 MD |
80 | } |
81 | ||
28ab034a | 82 | static const char *print_event_type(const enum lttng_event_type ev_type) |
9550ee81 JR |
83 | { |
84 | switch (ev_type) { | |
85 | case LTTNG_EVENT_ALL: | |
86 | return "any"; | |
87 | case LTTNG_EVENT_TRACEPOINT: | |
88 | return "tracepoint"; | |
89 | case LTTNG_EVENT_PROBE: | |
90 | return "probe"; | |
91 | case LTTNG_EVENT_FUNCTION: | |
92 | return "function"; | |
93 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
94 | return "function entry"; | |
95 | case LTTNG_EVENT_SYSCALL: | |
96 | return "syscall"; | |
97 | default: | |
98 | return ""; | |
99 | } | |
100 | } | |
101 | ||
e4d484a5 JRJ |
102 | /* Mi print a partial event. |
103 | * enabled is 0 or 1 | |
104 | * success is 0 or 1 | |
105 | */ | |
b53d4e59 | 106 | static int mi_print_event(const char *event_name, int enabled, int success) |
e4d484a5 JRJ |
107 | { |
108 | int ret; | |
109 | ||
a0377dfe FD |
110 | LTTNG_ASSERT(writer); |
111 | LTTNG_ASSERT(event_name); | |
e4d484a5 JRJ |
112 | |
113 | /* Open event element */ | |
114 | ret = mi_lttng_writer_open_element(writer, config_element_event); | |
115 | if (ret) { | |
116 | goto end; | |
117 | } | |
118 | ||
119 | /* Print the name of event */ | |
28ab034a | 120 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, event_name); |
e4d484a5 JRJ |
121 | if (ret) { |
122 | goto end; | |
123 | } | |
124 | ||
125 | /* Print enabled ? */ | |
28ab034a | 126 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); |
e4d484a5 JRJ |
127 | if (ret) { |
128 | goto end; | |
129 | } | |
130 | ||
131 | /* Success ? */ | |
28ab034a | 132 | ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_command_success, success); |
e4d484a5 JRJ |
133 | if (ret) { |
134 | goto end; | |
135 | } | |
136 | ||
137 | /* Close event element */ | |
138 | ret = mi_lttng_writer_close_element(writer); | |
139 | end: | |
140 | return ret; | |
141 | } | |
142 | ||
e953ef25 DG |
143 | /* |
144 | * disable_events | |
145 | * | |
146 | * Disabling event using the lttng API. | |
147 | */ | |
5b915816 | 148 | static int disable_events(char *session_name, char *event_list) |
e953ef25 | 149 | { |
e4d484a5 JRJ |
150 | int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS; |
151 | int enabled = 1, success = 1; | |
cd9adb8b | 152 | char *event_name, *channel_name = nullptr; |
7d29a247 | 153 | struct lttng_domain dom; |
6e911cad | 154 | struct lttng_event event; |
e953ef25 | 155 | |
441c16a7 MD |
156 | memset(&dom, 0, sizeof(dom)); |
157 | ||
d78d6610 | 158 | /* Create lttng domain */ |
7d29a247 DG |
159 | if (opt_kernel) { |
160 | dom.type = LTTNG_DOMAIN_KERNEL; | |
d78d6610 | 161 | } else if (opt_userspace) { |
9730260e | 162 | dom.type = LTTNG_DOMAIN_UST; |
b9dfb167 DG |
163 | } else if (opt_jul) { |
164 | dom.type = LTTNG_DOMAIN_JUL; | |
5cdb6027 DG |
165 | } else if (opt_log4j) { |
166 | dom.type = LTTNG_DOMAIN_LOG4J; | |
0e115563 DG |
167 | } else if (opt_python) { |
168 | dom.type = LTTNG_DOMAIN_PYTHON; | |
9730260e | 169 | } else { |
3ecec76a | 170 | /* Checked by the caller. */ |
a0377dfe | 171 | abort(); |
7d29a247 DG |
172 | } |
173 | ||
85076754 | 174 | channel_name = opt_channel_name; |
ae856491 | 175 | |
cd80958d | 176 | handle = lttng_create_handle(session_name, &dom); |
cd9adb8b | 177 | if (handle == nullptr) { |
cd80958d DG |
178 | ret = -1; |
179 | goto error; | |
180 | } | |
181 | ||
e4d484a5 JRJ |
182 | /* Mi print the channel and open the events element */ |
183 | if (lttng_opt_mi) { | |
184 | ret = mi_lttng_writer_open_element(writer, config_element_channel); | |
185 | if (ret) { | |
186 | ret = CMD_ERROR; | |
187 | goto end; | |
e953ef25 DG |
188 | } |
189 | ||
28ab034a JG |
190 | ret = mi_lttng_writer_write_element_string( |
191 | writer, config_element_name, print_channel_name(channel_name)); | |
e4d484a5 JRJ |
192 | if (ret) { |
193 | ret = CMD_ERROR; | |
194 | goto end; | |
195 | } | |
196 | ||
197 | /* Open events element */ | |
198 | ret = mi_lttng_writer_open_element(writer, config_element_events); | |
199 | if (ret) { | |
200 | ret = CMD_ERROR; | |
201 | goto end; | |
202 | } | |
e953ef25 DG |
203 | } |
204 | ||
6e911cad | 205 | memset(&event, 0, sizeof(event)); |
9b7431cf JG |
206 | /* Set default loglevel to any/unknown */ |
207 | event.loglevel = -1; | |
208 | ||
9550ee81 | 209 | /* opt_event_type contain the event type to disable at this point */ |
48a40005 | 210 | event.type = (lttng_event_type) opt_event_type; |
6e911cad | 211 | |
e4d484a5 | 212 | if (opt_disable_all) { |
cd9adb8b | 213 | command_ret = lttng_disable_event_ext(handle, &event, channel_name, nullptr); |
e4d484a5 JRJ |
214 | if (command_ret < 0) { |
215 | ERR("%s", lttng_strerror(command_ret)); | |
216 | enabled = 1; | |
217 | success = 0; | |
218 | ||
9730260e | 219 | } else { |
e4d484a5 JRJ |
220 | enabled = 0; |
221 | success = 1; | |
9550ee81 | 222 | MSG("All %s events of type %s are disabled in channel %s", |
28ab034a JG |
223 | lttng_domain_type_str(dom.type), |
224 | print_event_type((lttng_event_type) opt_event_type), | |
225 | print_channel_name(channel_name)); | |
9730260e DG |
226 | } |
227 | ||
e4d484a5 JRJ |
228 | if (lttng_opt_mi) { |
229 | ret = mi_print_event("*", enabled, success); | |
230 | if (ret) { | |
231 | ret = CMD_ERROR; | |
232 | goto error; | |
233 | } | |
234 | } | |
235 | } else { | |
236 | /* Strip event list */ | |
5b915816 | 237 | event_name = strtok(event_list, ","); |
cd9adb8b | 238 | while (event_name != nullptr) { |
e4d484a5 | 239 | DBG("Disabling event %s", event_name); |
e953ef25 | 240 | |
6e911cad MD |
241 | strncpy(event.name, event_name, sizeof(event.name)); |
242 | event.name[sizeof(event.name) - 1] = '\0'; | |
cd9adb8b JG |
243 | command_ret = |
244 | lttng_disable_event_ext(handle, &event, channel_name, nullptr); | |
e4d484a5 | 245 | if (command_ret < 0) { |
9550ee81 | 246 | ERR("%s of type %s : %s (channel %s, session %s)", |
28ab034a JG |
247 | event_name, |
248 | print_event_type((lttng_event_type) opt_event_type), | |
249 | lttng_strerror(command_ret), | |
250 | command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ? | |
251 | print_raw_channel_name(channel_name) : | |
252 | print_channel_name(channel_name), | |
253 | session_name); | |
e4d484a5 JRJ |
254 | warn = 1; |
255 | success = 0; | |
256 | /* | |
6e911cad MD |
257 | * If an error occurred we assume that the event is still |
258 | * enabled. | |
e4d484a5 JRJ |
259 | */ |
260 | enabled = 1; | |
261 | } else { | |
9550ee81 | 262 | MSG("%s %s of type %s disabled in channel %s for session %s", |
28ab034a JG |
263 | lttng_domain_type_str(dom.type), |
264 | event_name, | |
265 | print_event_type((lttng_event_type) opt_event_type), | |
266 | print_channel_name(channel_name), | |
267 | session_name); | |
e4d484a5 JRJ |
268 | success = 1; |
269 | enabled = 0; | |
270 | } | |
271 | ||
272 | if (lttng_opt_mi) { | |
273 | ret = mi_print_event(event_name, enabled, success); | |
274 | if (ret) { | |
275 | ret = CMD_ERROR; | |
276 | goto error; | |
277 | } | |
278 | } | |
279 | ||
280 | /* Next event */ | |
cd9adb8b | 281 | event_name = strtok(nullptr, ","); |
e4d484a5 JRJ |
282 | } |
283 | } | |
ae856491 | 284 | |
9730260e | 285 | end: |
e4d484a5 JRJ |
286 | if (lttng_opt_mi) { |
287 | /* Close events element and channel element */ | |
288 | ret = mi_lttng_close_multi_element(writer, 2); | |
289 | if (ret) { | |
290 | ret = CMD_ERROR; | |
291 | } | |
292 | } | |
e953ef25 | 293 | error: |
e4d484a5 JRJ |
294 | /* if there is already an error preserve it */ |
295 | if (warn && !ret) { | |
ae856491 DG |
296 | ret = CMD_WARNING; |
297 | } | |
cd80958d | 298 | |
e4d484a5 JRJ |
299 | /* Overwrite ret if an error occurred */ |
300 | ret = command_ret ? command_ret : ret; | |
301 | ||
302 | lttng_destroy_handle(handle); | |
e953ef25 DG |
303 | return ret; |
304 | } | |
305 | ||
306 | /* | |
307 | * cmd_disable_events | |
308 | * | |
309 | * Disable event to trace session | |
310 | */ | |
311 | int cmd_disable_events(int argc, const char **argv) | |
312 | { | |
e4d484a5 | 313 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
e953ef25 | 314 | static poptContext pc; |
cd9adb8b JG |
315 | char *session_name = nullptr; |
316 | char *event_list = nullptr; | |
317 | const char *arg_event_list = nullptr; | |
318 | const char *leftover = nullptr; | |
6e911cad | 319 | int event_type = -1; |
e953ef25 | 320 | |
cd9adb8b | 321 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
e953ef25 DG |
322 | poptReadDefaultConfig(pc, 0); |
323 | ||
6e911cad MD |
324 | /* Default event type */ |
325 | opt_event_type = LTTNG_EVENT_ALL; | |
326 | ||
e953ef25 DG |
327 | while ((opt = poptGetNextOpt(pc)) != -1) { |
328 | switch (opt) { | |
329 | case OPT_HELP: | |
4ba92f18 | 330 | SHOW_HELP(); |
e953ef25 | 331 | goto end; |
9550ee81 | 332 | case OPT_TYPE_SYSCALL: |
6e911cad MD |
333 | opt_event_type = LTTNG_EVENT_SYSCALL; |
334 | break; | |
9550ee81 JR |
335 | case OPT_TYPE_TRACEPOINT: |
336 | opt_event_type = LTTNG_EVENT_TRACEPOINT; | |
337 | break; | |
338 | case OPT_TYPE_PROBE: | |
339 | opt_event_type = LTTNG_EVENT_PROBE; | |
340 | break; | |
341 | case OPT_TYPE_FUNCTION: | |
342 | opt_event_type = LTTNG_EVENT_FUNCTION; | |
343 | break; | |
344 | case OPT_TYPE_ALL: | |
345 | opt_event_type = LTTNG_EVENT_ALL; | |
346 | break; | |
679b4943 SM |
347 | case OPT_LIST_OPTIONS: |
348 | list_cmd_options(stdout, long_options); | |
679b4943 | 349 | goto end; |
e953ef25 | 350 | default: |
e953ef25 DG |
351 | ret = CMD_UNDEFINED; |
352 | goto end; | |
353 | } | |
6e911cad MD |
354 | |
355 | /* Validate event type. Multiple event type are not supported. */ | |
356 | if (event_type == -1) { | |
357 | event_type = opt_event_type; | |
358 | } else { | |
359 | if (event_type != opt_event_type) { | |
360 | ERR("Multiple event type not supported."); | |
361 | ret = CMD_ERROR; | |
362 | goto end; | |
363 | } | |
364 | } | |
e953ef25 DG |
365 | } |
366 | ||
3ecec76a | 367 | ret = print_missing_or_multiple_domains( |
28ab034a | 368 | opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python, true); |
3ecec76a PP |
369 | if (ret) { |
370 | ret = CMD_ERROR; | |
371 | goto end; | |
372 | } | |
373 | ||
9550ee81 | 374 | /* Ust and agent only support ALL event type */ |
28ab034a JG |
375 | if ((opt_userspace || opt_jul || opt_log4j || opt_python) && |
376 | opt_event_type != LTTNG_EVENT_ALL) { | |
7767963f | 377 | ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n"); |
9550ee81 JR |
378 | ret = CMD_ERROR; |
379 | goto end; | |
380 | } | |
381 | ||
5b915816 | 382 | arg_event_list = poptGetArg(pc); |
cd9adb8b | 383 | if (arg_event_list == nullptr && opt_disable_all == 0) { |
e953ef25 | 384 | ERR("Missing event name(s).\n"); |
ca1c3607 | 385 | ret = CMD_ERROR; |
e953ef25 DG |
386 | goto end; |
387 | } | |
388 | ||
5b915816 MJ |
389 | if (opt_disable_all == 0) { |
390 | event_list = strdup(arg_event_list); | |
cd9adb8b | 391 | if (event_list == nullptr) { |
5b915816 MJ |
392 | PERROR("Failed to copy event name(s)"); |
393 | ret = CMD_ERROR; | |
394 | goto end; | |
395 | } | |
396 | } | |
397 | ||
68c7f6e5 JD |
398 | leftover = poptGetArg(pc); |
399 | if (leftover) { | |
400 | ERR("Unknown argument: %s", leftover); | |
401 | ret = CMD_ERROR; | |
402 | goto end; | |
403 | } | |
404 | ||
cd80958d DG |
405 | if (!opt_session_name) { |
406 | session_name = get_session_name(); | |
cd9adb8b | 407 | if (session_name == nullptr) { |
ca1c3607 | 408 | ret = CMD_ERROR; |
cd80958d DG |
409 | goto end; |
410 | } | |
411 | } else { | |
412 | session_name = opt_session_name; | |
413 | } | |
414 | ||
e4d484a5 JRJ |
415 | /* Mi check */ |
416 | if (lttng_opt_mi) { | |
417 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
418 | if (!writer) { | |
419 | ret = -LTTNG_ERR_NOMEM; | |
420 | goto end; | |
421 | } | |
422 | ||
423 | /* Open command element */ | |
28ab034a | 424 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_disable_event); |
e4d484a5 JRJ |
425 | if (ret) { |
426 | ret = CMD_ERROR; | |
427 | goto end; | |
428 | } | |
429 | ||
430 | /* Open output element */ | |
28ab034a | 431 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
e4d484a5 JRJ |
432 | if (ret) { |
433 | ret = CMD_ERROR; | |
434 | goto end; | |
435 | } | |
436 | } | |
437 | ||
5b915816 | 438 | command_ret = disable_events(session_name, event_list); |
e4d484a5 JRJ |
439 | if (command_ret) { |
440 | success = 0; | |
441 | } | |
442 | ||
443 | /* Mi closing */ | |
444 | if (lttng_opt_mi) { | |
445 | /* Close output element */ | |
446 | ret = mi_lttng_writer_close_element(writer); | |
447 | if (ret) { | |
448 | ret = CMD_ERROR; | |
449 | goto end; | |
450 | } | |
451 | ||
28ab034a JG |
452 | ret = mi_lttng_writer_write_element_bool( |
453 | writer, mi_lttng_element_command_success, success); | |
e4d484a5 JRJ |
454 | if (ret) { |
455 | ret = CMD_ERROR; | |
456 | goto end; | |
457 | } | |
458 | ||
459 | /* Command element close */ | |
460 | ret = mi_lttng_writer_command_close(writer); | |
461 | if (ret) { | |
462 | ret = CMD_ERROR; | |
463 | goto end; | |
464 | } | |
465 | } | |
e953ef25 DG |
466 | |
467 | end: | |
5853fd43 DG |
468 | if (!opt_session_name && session_name) { |
469 | free(session_name); | |
470 | } | |
e4d484a5 | 471 | |
5b915816 MJ |
472 | free(event_list); |
473 | ||
e4d484a5 JRJ |
474 | /* Mi clean-up */ |
475 | if (writer && mi_lttng_writer_destroy(writer)) { | |
476 | /* Preserve original error code */ | |
477 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
478 | } | |
479 | ||
83f4233d | 480 | /* Overwrite ret if an error occurred in disable_events */ |
e4d484a5 JRJ |
481 | ret = command_ret ? command_ret : ret; |
482 | ||
ca1c3607 | 483 | poptFreeContext(pc); |
e953ef25 DG |
484 | return ret; |
485 | } |