Cleanup: rename `get_domain_str()` -> `lttng_domain_type_str()`
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
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#include <assert.h>
17
18#include <common/mi-lttng.h>
1004b719 19#include <lttng/domain-internal.h>
e953ef25 20
c399183f 21#include "../command.h"
e953ef25
DG
22
23static char *opt_event_list;
9730260e 24static int opt_kernel;
e953ef25 25static char *opt_channel_name;
5440dc42 26static char *opt_session_name;
e953ef25
DG
27static int opt_userspace;
28static int opt_disable_all;
b9dfb167 29static int opt_jul;
5cdb6027 30static int opt_log4j;
0e115563 31static int opt_python;
6e911cad 32static int opt_event_type;
e953ef25 33
4fc83d94
PP
34#ifdef LTTNG_EMBED_HELP
35static const char help_msg[] =
36#include <lttng-disable-event.1.h>
37;
38#endif
39
e953ef25
DG
40enum {
41 OPT_HELP = 1,
9550ee81
JR
42 OPT_TYPE_SYSCALL,
43 OPT_TYPE_TRACEPOINT,
44 OPT_TYPE_PROBE,
45 OPT_TYPE_FUNCTION,
46 OPT_TYPE_ALL,
679b4943 47 OPT_LIST_OPTIONS,
e953ef25
DG
48};
49
cd80958d 50static struct lttng_handle *handle;
e4d484a5 51static struct mi_writer *writer;
cd80958d 52
e953ef25
DG
53static struct poptOption long_options[] = {
54 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
55 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 56 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e953ef25
DG
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},
b9dfb167 59 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 60 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 61 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
e953ef25 62 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
9550ee81
JR
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},
679b4943 69 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e953ef25
DG
70 {0, 0, 0, 0, 0, 0, 0}
71};
72
85076754
MD
73static
74const char *print_channel_name(const char *name)
75{
76 return name ? : DEFAULT_CHANNEL_NAME;
77}
78
79static
80const char *print_raw_channel_name(const char *name)
81{
82 return name ? : "<default>";
83}
84
9550ee81
JR
85static
86const 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
e4d484a5
JRJ
106/* Mi print a partial event.
107 * enabled is 0 or 1
108 * success is 0 or 1
109 */
b53d4e59 110static int mi_print_event(const char *event_name, int enabled, int success)
e4d484a5
JRJ
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);
146end:
147 return ret;
148}
149
e953ef25
DG
150/*
151 * disable_events
152 *
153 * Disabling event using the lttng API.
154 */
cd80958d 155static int disable_events(char *session_name)
e953ef25 156{
e4d484a5
JRJ
157 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
158 int enabled = 1, success = 1;
b73d0b29 159 char *event_name, *channel_name = NULL;
7d29a247 160 struct lttng_domain dom;
6e911cad 161 struct lttng_event event;
e953ef25 162
441c16a7
MD
163 memset(&dom, 0, sizeof(dom));
164
d78d6610 165 /* Create lttng domain */
7d29a247
DG
166 if (opt_kernel) {
167 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 168 } else if (opt_userspace) {
9730260e 169 dom.type = LTTNG_DOMAIN_UST;
b9dfb167
DG
170 } else if (opt_jul) {
171 dom.type = LTTNG_DOMAIN_JUL;
5cdb6027
DG
172 } else if (opt_log4j) {
173 dom.type = LTTNG_DOMAIN_LOG4J;
0e115563
DG
174 } else if (opt_python) {
175 dom.type = LTTNG_DOMAIN_PYTHON;
9730260e 176 } else {
3ecec76a
PP
177 /* Checked by the caller. */
178 assert(0);
7d29a247
DG
179 }
180
85076754 181 channel_name = opt_channel_name;
ae856491 182
cd80958d
DG
183 handle = lttng_create_handle(session_name, &dom);
184 if (handle == NULL) {
185 ret = -1;
186 goto error;
187 }
188
e4d484a5
JRJ
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;
e953ef25
DG
195 }
196
e4d484a5
JRJ
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 }
e953ef25
DG
210 }
211
6e911cad 212 memset(&event, 0, sizeof(event));
9b7431cf
JG
213 /* Set default loglevel to any/unknown */
214 event.loglevel = -1;
215
9550ee81
JR
216 /* opt_event_type contain the event type to disable at this point */
217 event.type = opt_event_type;
6e911cad 218
e4d484a5 219 if (opt_disable_all) {
6e911cad 220 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
e4d484a5
JRJ
221 if (command_ret < 0) {
222 ERR("%s", lttng_strerror(command_ret));
223 enabled = 1;
224 success = 0;
225
9730260e 226 } else {
e4d484a5
JRJ
227 enabled = 0;
228 success = 1;
9550ee81 229 MSG("All %s events of type %s are disabled in channel %s",
1004b719 230 lttng_domain_type_str(dom.type),
9550ee81 231 print_event_type(opt_event_type),
6e911cad 232 print_channel_name(channel_name));
9730260e
DG
233 }
234
e4d484a5
JRJ
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);
e953ef25 247
6e911cad
MD
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);
e4d484a5 251 if (command_ret < 0) {
9550ee81 252 ERR("%s of type %s : %s (channel %s, session %s)",
6e911cad 253 event_name,
9550ee81 254 print_event_type(opt_event_type),
e4d484a5
JRJ
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 /*
6e911cad
MD
263 * If an error occurred we assume that the event is still
264 * enabled.
e4d484a5
JRJ
265 */
266 enabled = 1;
267 } else {
9550ee81 268 MSG("%s %s of type %s disabled in channel %s for session %s",
1004b719 269 lttng_domain_type_str(dom.type),
6e911cad 270 event_name,
9550ee81 271 print_event_type(opt_event_type),
e4d484a5
JRJ
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 }
ae856491 290
9730260e 291end:
e4d484a5
JRJ
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 }
e953ef25 299error:
e4d484a5
JRJ
300 /* if there is already an error preserve it */
301 if (warn && !ret) {
ae856491
DG
302 ret = CMD_WARNING;
303 }
cd80958d 304
e4d484a5
JRJ
305 /* Overwrite ret if an error occurred */
306 ret = command_ret ? command_ret : ret;
307
308 lttng_destroy_handle(handle);
e953ef25
DG
309 return ret;
310}
311
312/*
313 * cmd_disable_events
314 *
315 * Disable event to trace session
316 */
317int cmd_disable_events(int argc, const char **argv)
318{
e4d484a5 319 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
e953ef25 320 static poptContext pc;
cd80958d 321 char *session_name = NULL;
68c7f6e5 322 const char *leftover = NULL;
6e911cad 323 int event_type = -1;
e953ef25
DG
324
325 pc = poptGetContext(NULL, argc, argv, long_options, 0);
326 poptReadDefaultConfig(pc, 0);
327
6e911cad
MD
328 /* Default event type */
329 opt_event_type = LTTNG_EVENT_ALL;
330
e953ef25
DG
331 while ((opt = poptGetNextOpt(pc)) != -1) {
332 switch (opt) {
333 case OPT_HELP:
4ba92f18 334 SHOW_HELP();
e953ef25 335 goto end;
9550ee81 336 case OPT_TYPE_SYSCALL:
6e911cad
MD
337 opt_event_type = LTTNG_EVENT_SYSCALL;
338 break;
9550ee81
JR
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;
679b4943
SM
351 case OPT_LIST_OPTIONS:
352 list_cmd_options(stdout, long_options);
679b4943 353 goto end;
e953ef25 354 default:
e953ef25
DG
355 ret = CMD_UNDEFINED;
356 goto end;
357 }
6e911cad
MD
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 }
e953ef25
DG
369 }
370
3ecec76a 371 ret = print_missing_or_multiple_domains(
3533d06b
JG
372 opt_kernel + opt_userspace + opt_jul + opt_log4j +
373 opt_python,
374 true);
3ecec76a
PP
375 if (ret) {
376 ret = CMD_ERROR;
377 goto end;
378 }
379
9550ee81
JR
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) {
7767963f 383 ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n");
9550ee81
JR
384 ret = CMD_ERROR;
385 goto end;
386 }
387
e953ef25
DG
388 opt_event_list = (char*) poptGetArg(pc);
389 if (opt_event_list == NULL && opt_disable_all == 0) {
390 ERR("Missing event name(s).\n");
ca1c3607 391 ret = CMD_ERROR;
e953ef25
DG
392 goto end;
393 }
394
68c7f6e5
JD
395 leftover = poptGetArg(pc);
396 if (leftover) {
397 ERR("Unknown argument: %s", leftover);
398 ret = CMD_ERROR;
399 goto end;
400 }
401
cd80958d
DG
402 if (!opt_session_name) {
403 session_name = get_session_name();
404 if (session_name == NULL) {
ca1c3607 405 ret = CMD_ERROR;
cd80958d
DG
406 goto end;
407 }
408 } else {
409 session_name = opt_session_name;
410 }
411
e4d484a5
JRJ
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 }
e953ef25
DG
465
466end:
5853fd43
DG
467 if (!opt_session_name && session_name) {
468 free(session_name);
469 }
e4d484a5
JRJ
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
83f4233d 477 /* Overwrite ret if an error occurred in disable_events */
e4d484a5
JRJ
478 ret = command_ret ? command_ret : ret;
479
ca1c3607 480 poptFreeContext(pc);
e953ef25
DG
481 return ret;
482}
This page took 0.125299 seconds and 4 git commands to generate.