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