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