configure: enable -Wformat=2
[lttng-tools.git] / src / bin / lttng / commands / disable_events.cpp
CommitLineData
e953ef25 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
e953ef25 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
e953ef25 5 *
e953ef25
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
e953ef25
DG
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>
e4d484a5
JRJ
16
17#include <common/mi-lttng.h>
1004b719 18#include <lttng/domain-internal.h>
e953ef25 19
c399183f 20#include "../command.h"
e953ef25
DG
21
22static char *opt_event_list;
9730260e 23static int opt_kernel;
e953ef25 24static char *opt_channel_name;
5440dc42 25static char *opt_session_name;
e953ef25
DG
26static int opt_userspace;
27static int opt_disable_all;
b9dfb167 28static int opt_jul;
5cdb6027 29static int opt_log4j;
0e115563 30static int opt_python;
6e911cad 31static int opt_event_type;
e953ef25 32
4fc83d94
PP
33#ifdef LTTNG_EMBED_HELP
34static const char help_msg[] =
35#include <lttng-disable-event.1.h>
36;
37#endif
38
e953ef25
DG
39enum {
40 OPT_HELP = 1,
9550ee81
JR
41 OPT_TYPE_SYSCALL,
42 OPT_TYPE_TRACEPOINT,
43 OPT_TYPE_PROBE,
44 OPT_TYPE_FUNCTION,
45 OPT_TYPE_ALL,
679b4943 46 OPT_LIST_OPTIONS,
e953ef25
DG
47};
48
cd80958d 49static struct lttng_handle *handle;
e4d484a5 50static struct mi_writer *writer;
cd80958d 51
e953ef25
DG
52static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e953ef25
DG
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},
b9dfb167 58 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 59 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 60 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
e953ef25 61 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
9550ee81
JR
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},
679b4943 68 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e953ef25
DG
69 {0, 0, 0, 0, 0, 0, 0}
70};
71
85076754
MD
72static
73const char *print_channel_name(const char *name)
74{
75 return name ? : DEFAULT_CHANNEL_NAME;
76}
77
78static
79const char *print_raw_channel_name(const char *name)
80{
81 return name ? : "<default>";
82}
83
9550ee81
JR
84static
85const 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
e4d484a5
JRJ
105/* Mi print a partial event.
106 * enabled is 0 or 1
107 * success is 0 or 1
108 */
b53d4e59 109static int mi_print_event(const char *event_name, int enabled, int success)
e4d484a5
JRJ
110{
111 int ret;
112
a0377dfe
FD
113 LTTNG_ASSERT(writer);
114 LTTNG_ASSERT(event_name);
e4d484a5
JRJ
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);
145end:
146 return ret;
147}
148
e953ef25
DG
149/*
150 * disable_events
151 *
152 * Disabling event using the lttng API.
153 */
cd80958d 154static int disable_events(char *session_name)
e953ef25 155{
e4d484a5
JRJ
156 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
157 int enabled = 1, success = 1;
b73d0b29 158 char *event_name, *channel_name = NULL;
7d29a247 159 struct lttng_domain dom;
6e911cad 160 struct lttng_event event;
e953ef25 161
441c16a7
MD
162 memset(&dom, 0, sizeof(dom));
163
d78d6610 164 /* Create lttng domain */
7d29a247
DG
165 if (opt_kernel) {
166 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 167 } else if (opt_userspace) {
9730260e 168 dom.type = LTTNG_DOMAIN_UST;
b9dfb167
DG
169 } else if (opt_jul) {
170 dom.type = LTTNG_DOMAIN_JUL;
5cdb6027
DG
171 } else if (opt_log4j) {
172 dom.type = LTTNG_DOMAIN_LOG4J;
0e115563
DG
173 } else if (opt_python) {
174 dom.type = LTTNG_DOMAIN_PYTHON;
9730260e 175 } else {
3ecec76a 176 /* Checked by the caller. */
a0377dfe 177 abort();
7d29a247
DG
178 }
179
85076754 180 channel_name = opt_channel_name;
ae856491 181
cd80958d
DG
182 handle = lttng_create_handle(session_name, &dom);
183 if (handle == NULL) {
184 ret = -1;
185 goto error;
186 }
187
e4d484a5
JRJ
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;
e953ef25
DG
194 }
195
e4d484a5
JRJ
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 }
e953ef25
DG
209 }
210
6e911cad 211 memset(&event, 0, sizeof(event));
9b7431cf
JG
212 /* Set default loglevel to any/unknown */
213 event.loglevel = -1;
214
9550ee81 215 /* opt_event_type contain the event type to disable at this point */
48a40005 216 event.type = (lttng_event_type) opt_event_type;
6e911cad 217
e4d484a5 218 if (opt_disable_all) {
6e911cad 219 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
e4d484a5
JRJ
220 if (command_ret < 0) {
221 ERR("%s", lttng_strerror(command_ret));
222 enabled = 1;
223 success = 0;
224
9730260e 225 } else {
e4d484a5
JRJ
226 enabled = 0;
227 success = 1;
9550ee81 228 MSG("All %s events of type %s are disabled in channel %s",
1004b719 229 lttng_domain_type_str(dom.type),
48a40005 230 print_event_type((lttng_event_type) opt_event_type),
6e911cad 231 print_channel_name(channel_name));
9730260e
DG
232 }
233
e4d484a5
JRJ
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);
e953ef25 246
6e911cad
MD
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);
e4d484a5 250 if (command_ret < 0) {
9550ee81 251 ERR("%s of type %s : %s (channel %s, session %s)",
6e911cad 252 event_name,
48a40005 253 print_event_type((lttng_event_type) opt_event_type),
e4d484a5
JRJ
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 /*
6e911cad
MD
262 * If an error occurred we assume that the event is still
263 * enabled.
e4d484a5
JRJ
264 */
265 enabled = 1;
266 } else {
9550ee81 267 MSG("%s %s of type %s disabled in channel %s for session %s",
1004b719 268 lttng_domain_type_str(dom.type),
6e911cad 269 event_name,
48a40005 270 print_event_type((lttng_event_type) opt_event_type),
e4d484a5
JRJ
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 }
ae856491 289
9730260e 290end:
e4d484a5
JRJ
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 }
e953ef25 298error:
e4d484a5
JRJ
299 /* if there is already an error preserve it */
300 if (warn && !ret) {
ae856491
DG
301 ret = CMD_WARNING;
302 }
cd80958d 303
e4d484a5
JRJ
304 /* Overwrite ret if an error occurred */
305 ret = command_ret ? command_ret : ret;
306
307 lttng_destroy_handle(handle);
e953ef25
DG
308 return ret;
309}
310
311/*
312 * cmd_disable_events
313 *
314 * Disable event to trace session
315 */
316int cmd_disable_events(int argc, const char **argv)
317{
e4d484a5 318 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
e953ef25 319 static poptContext pc;
cd80958d 320 char *session_name = NULL;
68c7f6e5 321 const char *leftover = NULL;
6e911cad 322 int event_type = -1;
e953ef25
DG
323
324 pc = poptGetContext(NULL, argc, argv, long_options, 0);
325 poptReadDefaultConfig(pc, 0);
326
6e911cad
MD
327 /* Default event type */
328 opt_event_type = LTTNG_EVENT_ALL;
329
e953ef25
DG
330 while ((opt = poptGetNextOpt(pc)) != -1) {
331 switch (opt) {
332 case OPT_HELP:
4ba92f18 333 SHOW_HELP();
e953ef25 334 goto end;
9550ee81 335 case OPT_TYPE_SYSCALL:
6e911cad
MD
336 opt_event_type = LTTNG_EVENT_SYSCALL;
337 break;
9550ee81
JR
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;
679b4943
SM
350 case OPT_LIST_OPTIONS:
351 list_cmd_options(stdout, long_options);
679b4943 352 goto end;
e953ef25 353 default:
e953ef25
DG
354 ret = CMD_UNDEFINED;
355 goto end;
356 }
6e911cad
MD
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 }
e953ef25
DG
368 }
369
3ecec76a 370 ret = print_missing_or_multiple_domains(
3533d06b
JG
371 opt_kernel + opt_userspace + opt_jul + opt_log4j +
372 opt_python,
373 true);
3ecec76a
PP
374 if (ret) {
375 ret = CMD_ERROR;
376 goto end;
377 }
378
9550ee81
JR
379 /* Ust and agent only support ALL event type */
380 if ((opt_userspace || opt_jul || opt_log4j || opt_python)
381 && opt_event_type != LTTNG_EVENT_ALL) {
7767963f 382 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
9550ee81
JR
383 ret = CMD_ERROR;
384 goto end;
385 }
386
e953ef25
DG
387 opt_event_list = (char*) poptGetArg(pc);
388 if (opt_event_list == NULL && opt_disable_all == 0) {
389 ERR("Missing event name(s).\n");
ca1c3607 390 ret = CMD_ERROR;
e953ef25
DG
391 goto end;
392 }
393
68c7f6e5
JD
394 leftover = poptGetArg(pc);
395 if (leftover) {
396 ERR("Unknown argument: %s", leftover);
397 ret = CMD_ERROR;
398 goto end;
399 }
400
cd80958d
DG
401 if (!opt_session_name) {
402 session_name = get_session_name();
403 if (session_name == NULL) {
ca1c3607 404 ret = CMD_ERROR;
cd80958d
DG
405 goto end;
406 }
407 } else {
408 session_name = opt_session_name;
409 }
410
e4d484a5
JRJ
411 /* Mi check */
412 if (lttng_opt_mi) {
413 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
414 if (!writer) {
415 ret = -LTTNG_ERR_NOMEM;
416 goto end;
417 }
418
419 /* Open command element */
420 ret = mi_lttng_writer_command_open(writer,
421 mi_lttng_element_command_disable_event);
422 if (ret) {
423 ret = CMD_ERROR;
424 goto end;
425 }
426
427 /* Open output element */
428 ret = mi_lttng_writer_open_element(writer,
429 mi_lttng_element_command_output);
430 if (ret) {
431 ret = CMD_ERROR;
432 goto end;
433 }
434 }
435
436 command_ret = disable_events(session_name);
437 if (command_ret) {
438 success = 0;
439 }
440
441 /* Mi closing */
442 if (lttng_opt_mi) {
443 /* Close output element */
444 ret = mi_lttng_writer_close_element(writer);
445 if (ret) {
446 ret = CMD_ERROR;
447 goto end;
448 }
449
450 ret = mi_lttng_writer_write_element_bool(writer,
451 mi_lttng_element_command_success, success);
452 if (ret) {
453 ret = CMD_ERROR;
454 goto end;
455 }
456
457 /* Command element close */
458 ret = mi_lttng_writer_command_close(writer);
459 if (ret) {
460 ret = CMD_ERROR;
461 goto end;
462 }
463 }
e953ef25
DG
464
465end:
5853fd43
DG
466 if (!opt_session_name && session_name) {
467 free(session_name);
468 }
e4d484a5
JRJ
469
470 /* Mi clean-up */
471 if (writer && mi_lttng_writer_destroy(writer)) {
472 /* Preserve original error code */
473 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
474 }
475
83f4233d 476 /* Overwrite ret if an error occurred in disable_events */
e4d484a5
JRJ
477 ret = command_ret ? command_ret : ret;
478
ca1c3607 479 poptFreeContext(pc);
e953ef25
DG
480 return ret;
481}
This page took 0.09484 seconds and 4 git commands to generate.