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