Set consumer's verbosity to the max level on --verbose-consumer
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _LGPL_SOURCE
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>
26 #include <assert.h>
27
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32 static char *opt_event_list;
33 static int opt_kernel;
34 static char *opt_channel_name;
35 static char *opt_session_name;
36 static int opt_userspace;
37 static int opt_disable_all;
38 static int opt_jul;
39 static int opt_log4j;
40 static int opt_python;
41 static int opt_event_type;
42
43 #ifdef LTTNG_EMBED_HELP
44 static const char help_msg[] =
45 #include <lttng-disable-event.1.h>
46 ;
47 #endif
48
49 enum {
50 OPT_HELP = 1,
51 OPT_TYPE_SYSCALL,
52 OPT_TYPE_TRACEPOINT,
53 OPT_TYPE_PROBE,
54 OPT_TYPE_FUNCTION,
55 OPT_TYPE_ALL,
56 OPT_LIST_OPTIONS,
57 };
58
59 static struct lttng_handle *handle;
60 static struct mi_writer *writer;
61
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},
65 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
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},
68 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
69 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
70 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
71 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
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},
78 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
79 {0, 0, 0, 0, 0, 0, 0}
80 };
81
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
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
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
159 /*
160 * disable_events
161 *
162 * Disabling event using the lttng API.
163 */
164 static int disable_events(char *session_name)
165 {
166 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
167 int enabled = 1, success = 1;
168 char *event_name, *channel_name = NULL;
169 struct lttng_domain dom;
170 struct lttng_event event;
171
172 memset(&dom, 0, sizeof(dom));
173
174 /* Create lttng domain */
175 if (opt_kernel) {
176 dom.type = LTTNG_DOMAIN_KERNEL;
177 } else if (opt_userspace) {
178 dom.type = LTTNG_DOMAIN_UST;
179 } else if (opt_jul) {
180 dom.type = LTTNG_DOMAIN_JUL;
181 } else if (opt_log4j) {
182 dom.type = LTTNG_DOMAIN_LOG4J;
183 } else if (opt_python) {
184 dom.type = LTTNG_DOMAIN_PYTHON;
185 } else {
186 /* Checked by the caller. */
187 assert(0);
188 }
189
190 channel_name = opt_channel_name;
191
192 handle = lttng_create_handle(session_name, &dom);
193 if (handle == NULL) {
194 ret = -1;
195 goto error;
196 }
197
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;
204 }
205
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 }
219 }
220
221 memset(&event, 0, sizeof(event));
222 /* Set default loglevel to any/unknown */
223 event.loglevel = -1;
224
225 /* opt_event_type contain the event type to disable at this point */
226 event.type = opt_event_type;
227
228 if (opt_disable_all) {
229 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
230 if (command_ret < 0) {
231 ERR("%s", lttng_strerror(command_ret));
232 enabled = 1;
233 success = 0;
234
235 } else {
236 enabled = 0;
237 success = 1;
238 MSG("All %s events of type %s are disabled in channel %s",
239 get_domain_str(dom.type),
240 print_event_type(opt_event_type),
241 print_channel_name(channel_name));
242 }
243
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);
256
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);
260 if (command_ret < 0) {
261 ERR("%s of type %s : %s (channel %s, session %s)",
262 event_name,
263 print_event_type(opt_event_type),
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 /*
272 * If an error occurred we assume that the event is still
273 * enabled.
274 */
275 enabled = 1;
276 } else {
277 MSG("%s %s of type %s disabled in channel %s for session %s",
278 get_domain_str(dom.type),
279 event_name,
280 print_event_type(opt_event_type),
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 }
299
300 end:
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 }
308 error:
309 /* if there is already an error preserve it */
310 if (warn && !ret) {
311 ret = CMD_WARNING;
312 }
313
314 /* Overwrite ret if an error occurred */
315 ret = command_ret ? command_ret : ret;
316
317 lttng_destroy_handle(handle);
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 {
328 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
329 static poptContext pc;
330 char *session_name = NULL;
331 const char *leftover = NULL;
332 int event_type = -1;
333
334 pc = poptGetContext(NULL, argc, argv, long_options, 0);
335 poptReadDefaultConfig(pc, 0);
336
337 /* Default event type */
338 opt_event_type = LTTNG_EVENT_ALL;
339
340 while ((opt = poptGetNextOpt(pc)) != -1) {
341 switch (opt) {
342 case OPT_HELP:
343 SHOW_HELP();
344 goto end;
345 case OPT_TYPE_SYSCALL:
346 opt_event_type = LTTNG_EVENT_SYSCALL;
347 break;
348 case OPT_TYPE_TRACEPOINT:
349 opt_event_type = LTTNG_EVENT_TRACEPOINT;
350 break;
351 case OPT_TYPE_PROBE:
352 opt_event_type = LTTNG_EVENT_PROBE;
353 break;
354 case OPT_TYPE_FUNCTION:
355 opt_event_type = LTTNG_EVENT_FUNCTION;
356 break;
357 case OPT_TYPE_ALL:
358 opt_event_type = LTTNG_EVENT_ALL;
359 break;
360 case OPT_LIST_OPTIONS:
361 list_cmd_options(stdout, long_options);
362 goto end;
363 default:
364 ret = CMD_UNDEFINED;
365 goto end;
366 }
367
368 /* Validate event type. Multiple event type are not supported. */
369 if (event_type == -1) {
370 event_type = opt_event_type;
371 } else {
372 if (event_type != opt_event_type) {
373 ERR("Multiple event type not supported.");
374 ret = CMD_ERROR;
375 goto end;
376 }
377 }
378 }
379
380 ret = print_missing_or_multiple_domains(
381 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
382 if (ret) {
383 ret = CMD_ERROR;
384 goto end;
385 }
386
387 /* Ust and agent only support ALL event type */
388 if ((opt_userspace || opt_jul || opt_log4j || opt_python)
389 && opt_event_type != LTTNG_EVENT_ALL) {
390 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
391 ret = CMD_ERROR;
392 goto end;
393 }
394
395 opt_event_list = (char*) poptGetArg(pc);
396 if (opt_event_list == NULL && opt_disable_all == 0) {
397 ERR("Missing event name(s).\n");
398 ret = CMD_ERROR;
399 goto end;
400 }
401
402 leftover = poptGetArg(pc);
403 if (leftover) {
404 ERR("Unknown argument: %s", leftover);
405 ret = CMD_ERROR;
406 goto end;
407 }
408
409 if (!opt_session_name) {
410 session_name = get_session_name();
411 if (session_name == NULL) {
412 ret = CMD_ERROR;
413 goto end;
414 }
415 } else {
416 session_name = opt_session_name;
417 }
418
419 /* Mi check */
420 if (lttng_opt_mi) {
421 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
422 if (!writer) {
423 ret = -LTTNG_ERR_NOMEM;
424 goto end;
425 }
426
427 /* Open command element */
428 ret = mi_lttng_writer_command_open(writer,
429 mi_lttng_element_command_disable_event);
430 if (ret) {
431 ret = CMD_ERROR;
432 goto end;
433 }
434
435 /* Open output element */
436 ret = mi_lttng_writer_open_element(writer,
437 mi_lttng_element_command_output);
438 if (ret) {
439 ret = CMD_ERROR;
440 goto end;
441 }
442 }
443
444 command_ret = disable_events(session_name);
445 if (command_ret) {
446 success = 0;
447 }
448
449 /* Mi closing */
450 if (lttng_opt_mi) {
451 /* Close output element */
452 ret = mi_lttng_writer_close_element(writer);
453 if (ret) {
454 ret = CMD_ERROR;
455 goto end;
456 }
457
458 ret = mi_lttng_writer_write_element_bool(writer,
459 mi_lttng_element_command_success, success);
460 if (ret) {
461 ret = CMD_ERROR;
462 goto end;
463 }
464
465 /* Command element close */
466 ret = mi_lttng_writer_command_close(writer);
467 if (ret) {
468 ret = CMD_ERROR;
469 goto end;
470 }
471 }
472
473 end:
474 if (!opt_session_name && session_name) {
475 free(session_name);
476 }
477
478 /* Mi clean-up */
479 if (writer && mi_lttng_writer_destroy(writer)) {
480 /* Preserve original error code */
481 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
482 }
483
484 /* Overwrite ret if an error occurred in disable_events */
485 ret = command_ret ? command_ret : ret;
486
487 poptFreeContext(pc);
488 return ret;
489 }
This page took 0.040053 seconds and 4 git commands to generate.