Clean-up: consumer.hpp: coding style indentation fix
[lttng-tools.git] / src / bin / lttng / commands / disable_events.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "../command.hpp"
10
11 #include <common/mi-lttng.hpp>
12
13 #include <lttng/domain-internal.hpp>
14
15 #include <popt.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
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, nullptr, OPT_HELP, nullptr, nullptr },
55 { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr },
56 { "all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, nullptr, nullptr },
57 { "channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, nullptr, nullptr },
58 { "jul", 'j', POPT_ARG_VAL, &opt_jul, 1, nullptr, nullptr },
59 { "log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, nullptr, nullptr },
60 { "python", 'p', POPT_ARG_VAL, &opt_python, 1, nullptr, nullptr },
61 { "kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, nullptr, nullptr },
62 { "userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, nullptr, nullptr },
63 { "syscall", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_SYSCALL, nullptr, nullptr },
64 { "probe", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_PROBE, nullptr, nullptr },
65 { "tracepoint", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_TRACEPOINT, nullptr, nullptr },
66 { "function", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_FUNCTION, nullptr, nullptr },
67 { "all", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_ALL, nullptr, nullptr },
68 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
69 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
70 };
71
72 static const char *print_channel_name(const char *name)
73 {
74 return name ?: DEFAULT_CHANNEL_NAME;
75 }
76
77 static const char *print_raw_channel_name(const char *name)
78 {
79 return name ?: "<default>";
80 }
81
82 static const char *print_event_type(const enum lttng_event_type ev_type)
83 {
84 switch (ev_type) {
85 case LTTNG_EVENT_ALL:
86 return "any";
87 case LTTNG_EVENT_TRACEPOINT:
88 return "tracepoint";
89 case LTTNG_EVENT_PROBE:
90 return "probe";
91 case LTTNG_EVENT_FUNCTION:
92 return "function";
93 case LTTNG_EVENT_FUNCTION_ENTRY:
94 return "function entry";
95 case LTTNG_EVENT_SYSCALL:
96 return "syscall";
97 default:
98 return "";
99 }
100 }
101
102 /* Mi print a partial event.
103 * enabled is 0 or 1
104 * success is 0 or 1
105 */
106 static int mi_print_event(const char *event_name, int enabled, int success)
107 {
108 int ret;
109
110 LTTNG_ASSERT(writer);
111 LTTNG_ASSERT(event_name);
112
113 /* Open event element */
114 ret = mi_lttng_writer_open_element(writer, config_element_event);
115 if (ret) {
116 goto end;
117 }
118
119 /* Print the name of event */
120 ret = mi_lttng_writer_write_element_string(writer, config_element_name, event_name);
121 if (ret) {
122 goto end;
123 }
124
125 /* Print enabled ? */
126 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
127 if (ret) {
128 goto end;
129 }
130
131 /* Success ? */
132 ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_command_success, success);
133 if (ret) {
134 goto end;
135 }
136
137 /* Close event element */
138 ret = mi_lttng_writer_close_element(writer);
139 end:
140 return ret;
141 }
142
143 /*
144 * disable_events
145 *
146 * Disabling event using the lttng API.
147 */
148 static int disable_events(char *session_name, char *event_list)
149 {
150 enum cmd_error_code ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
151 bool enabled = true, success = true, warn = false;
152 char *event_name, *channel_name = nullptr;
153 struct lttng_domain dom;
154 struct lttng_event event;
155
156 memset(&dom, 0, sizeof(dom));
157
158 /* Create lttng domain */
159 if (opt_kernel) {
160 dom.type = LTTNG_DOMAIN_KERNEL;
161 } else if (opt_userspace) {
162 dom.type = LTTNG_DOMAIN_UST;
163 } else if (opt_jul) {
164 dom.type = LTTNG_DOMAIN_JUL;
165 } else if (opt_log4j) {
166 dom.type = LTTNG_DOMAIN_LOG4J;
167 } else if (opt_python) {
168 dom.type = LTTNG_DOMAIN_PYTHON;
169 } else {
170 /* Checked by the caller. */
171 abort();
172 }
173
174 channel_name = opt_channel_name;
175
176 handle = lttng_create_handle(session_name, &dom);
177 if (handle == nullptr) {
178 ret = CMD_ERROR;
179 goto error;
180 }
181
182 /* Mi print the channel and open the events element */
183 if (lttng_opt_mi) {
184 int mi_ret = mi_lttng_writer_open_element(writer, config_element_channel);
185 if (mi_ret) {
186 ret = CMD_ERROR;
187 goto end;
188 }
189
190 mi_ret = mi_lttng_writer_write_element_string(
191 writer, config_element_name, print_channel_name(channel_name));
192 if (mi_ret) {
193 ret = CMD_ERROR;
194 goto end;
195 }
196
197 /* Open events element */
198 mi_ret = mi_lttng_writer_open_element(writer, config_element_events);
199 if (mi_ret) {
200 ret = CMD_ERROR;
201 goto end;
202 }
203 }
204
205 memset(&event, 0, sizeof(event));
206 /* Set default loglevel to any/unknown */
207 event.loglevel = -1;
208
209 /* opt_event_type contain the event type to disable at this point */
210 event.type = (lttng_event_type) opt_event_type;
211
212 if (opt_disable_all) {
213 const int disable_ret =
214 lttng_disable_event_ext(handle, &event, channel_name, nullptr);
215
216 if (disable_ret < 0) {
217 ERR("%s", lttng_strerror(command_ret));
218 command_ret = CMD_ERROR;
219 enabled = true;
220 success = false;
221 } else {
222 enabled = false;
223 success = true;
224 MSG("All %s events of type %s are disabled in channel %s",
225 lttng_domain_type_str(dom.type),
226 print_event_type((lttng_event_type) opt_event_type),
227 print_channel_name(channel_name));
228 }
229
230 if (lttng_opt_mi) {
231 const int mi_ret = mi_print_event("*", enabled, success);
232
233 if (mi_ret) {
234 ret = CMD_ERROR;
235 goto error;
236 }
237 }
238 } else {
239 /* Strip event list */
240 event_name = strtok(event_list, ",");
241 while (event_name != nullptr) {
242 DBG("Disabling event %s", event_name);
243
244 strncpy(event.name, event_name, sizeof(event.name));
245 event.name[sizeof(event.name) - 1] = '\0';
246 const int disable_ret =
247 lttng_disable_event_ext(handle, &event, channel_name, nullptr);
248 if (disable_ret < 0) {
249 ERR("%s of type %s : %s (channel %s, session %s)",
250 event_name,
251 print_event_type((lttng_event_type) opt_event_type),
252 lttng_strerror(disable_ret),
253 disable_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ?
254 print_raw_channel_name(channel_name) :
255 print_channel_name(channel_name),
256 session_name);
257 warn = true;
258 success = false;
259 /*
260 * If an error occurred we assume that the event is still
261 * enabled.
262 */
263 enabled = true;
264 command_ret = CMD_ERROR;
265 } else {
266 MSG("%s %s of type %s disabled in channel %s for session %s",
267 lttng_domain_type_str(dom.type),
268 event_name,
269 print_event_type((lttng_event_type) opt_event_type),
270 print_channel_name(channel_name),
271 session_name);
272 success = true;
273 enabled = false;
274 }
275
276 if (lttng_opt_mi) {
277 const int mi_ret = mi_print_event(event_name, enabled, success);
278
279 if (mi_ret) {
280 ret = CMD_ERROR;
281 goto error;
282 }
283 }
284
285 /* Next event */
286 event_name = strtok(nullptr, ",");
287 }
288 }
289
290 end:
291 if (lttng_opt_mi) {
292 /* Close events element and channel element */
293 const int mi_ret = mi_lttng_close_multi_element(writer, 2);
294
295 if (mi_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 = nullptr;
322 char *event_list = nullptr;
323 const char *arg_event_list = nullptr;
324 const char *leftover = nullptr;
325 int event_type = -1;
326
327 pc = poptGetContext(nullptr, 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, 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 arg_event_list = poptGetArg(pc);
389 if (arg_event_list == nullptr && opt_disable_all == 0) {
390 ERR("Missing event name(s).\n");
391 ret = CMD_ERROR;
392 goto end;
393 }
394
395 if (opt_disable_all == 0) {
396 event_list = strdup(arg_event_list);
397 if (event_list == nullptr) {
398 PERROR("Failed to copy event name(s)");
399 ret = CMD_ERROR;
400 goto end;
401 }
402 }
403
404 leftover = poptGetArg(pc);
405 if (leftover) {
406 ERR("Unknown argument: %s", leftover);
407 ret = CMD_ERROR;
408 goto end;
409 }
410
411 if (!opt_session_name) {
412 session_name = get_session_name();
413 if (session_name == nullptr) {
414 ret = CMD_ERROR;
415 goto end;
416 }
417 } else {
418 session_name = opt_session_name;
419 }
420
421 /* Mi check */
422 if (lttng_opt_mi) {
423 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
424 if (!writer) {
425 ret = -LTTNG_ERR_NOMEM;
426 goto end;
427 }
428
429 /* Open command element */
430 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_disable_event);
431 if (ret) {
432 ret = CMD_ERROR;
433 goto end;
434 }
435
436 /* Open output element */
437 ret = mi_lttng_writer_open_element(writer, 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, event_list);
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(
459 writer, 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 free(event_list);
479
480 /* Mi clean-up */
481 if (writer && mi_lttng_writer_destroy(writer)) {
482 /* Preserve original error code */
483 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
484 }
485
486 /* Overwrite ret if an error occurred in disable_events */
487 ret = command_ret ? command_ret : ret;
488
489 poptFreeContext(pc);
490 return ret;
491 }
This page took 0.038997 seconds and 4 git commands to generate.