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