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