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