Fix: HT must not be destroyed with a rcu_read_lock held
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
CommitLineData
e953ef25
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
e953ef25
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e953ef25
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
e4d484a5
JRJ
26#include <assert.h>
27
28#include <common/mi-lttng.h>
e953ef25 29
c399183f 30#include "../command.h"
e953ef25
DG
31
32static char *opt_event_list;
9730260e 33static int opt_kernel;
e953ef25 34static char *opt_channel_name;
5440dc42 35static char *opt_session_name;
e953ef25
DG
36static int opt_userspace;
37static int opt_disable_all;
b9dfb167 38static int opt_jul;
5cdb6027 39static int opt_log4j;
0e115563 40static int opt_python;
6e911cad 41static int opt_event_type;
d78d6610
DG
42#if 0
43/* Not implemented yet */
44static char *opt_cmd_name;
e953ef25 45static pid_t opt_pid;
d78d6610 46#endif
e953ef25
DG
47
48enum {
49 OPT_HELP = 1,
50 OPT_USERSPACE,
6e911cad 51 OPT_SYSCALL,
679b4943 52 OPT_LIST_OPTIONS,
e953ef25
DG
53};
54
cd80958d 55static struct lttng_handle *handle;
e4d484a5 56static struct mi_writer *writer;
cd80958d 57
e953ef25
DG
58static struct poptOption long_options[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 61 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e953ef25
DG
62 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
63 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
b9dfb167 64 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 65 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 66 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
e953ef25 67 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
6e911cad 68 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
d78d6610
DG
69#if 0
70 /* Not implemented yet */
6caa2bcc 71 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
e953ef25 72 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
73#else
74 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
75#endif
679b4943 76 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e953ef25
DG
77 {0, 0, 0, 0, 0, 0, 0}
78};
79
80/*
81 * usage
82 */
83static void usage(FILE *ofp)
84{
c138a39b 85 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
e953ef25 86 fprintf(ofp, "\n");
32a6298d 87 fprintf(ofp, "Options:\n");
e953ef25 88 fprintf(ofp, " -h, --help Show this help\n");
679b4943 89 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d
DG
90 fprintf(ofp, " -s, --session NAME Apply to session name\n");
91 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
9730260e 92 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
e953ef25 93 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
27221701 94 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
b9dfb167 95 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
5cdb6027 96 fprintf(ofp, " -l, --log4j Apply to Java application using LOG4j\n");
0e115563 97 fprintf(ofp, " -p, --python Apply to Python application using logging\n");
e953ef25 98 fprintf(ofp, "\n");
6e911cad
MD
99 fprintf(ofp, "Event options:\n");
100 fprintf(ofp, " --syscall System call event\n");
101 fprintf(ofp, "\n");
e953ef25
DG
102}
103
85076754
MD
104static
105const char *print_channel_name(const char *name)
106{
107 return name ? : DEFAULT_CHANNEL_NAME;
108}
109
110static
111const char *print_raw_channel_name(const char *name)
112{
113 return name ? : "<default>";
114}
115
e4d484a5
JRJ
116/* Mi print a partial event.
117 * enabled is 0 or 1
118 * success is 0 or 1
119 */
120static int mi_print_event(char *event_name, int enabled, int success)
121{
122 int ret;
123
124 assert(writer);
125 assert(event_name);
126
127 /* Open event element */
128 ret = mi_lttng_writer_open_element(writer, config_element_event);
129 if (ret) {
130 goto end;
131 }
132
133 /* Print the name of event */
134 ret = mi_lttng_writer_write_element_string(writer,
135 config_element_name, event_name);
136 if (ret) {
137 goto end;
138 }
139
140 /* Print enabled ? */
141 ret = mi_lttng_writer_write_element_bool(writer,
142 config_element_enabled, enabled);
143 if (ret) {
144 goto end;
145 }
146
147 /* Success ? */
148 ret = mi_lttng_writer_write_element_bool(writer,
149 mi_lttng_element_command_success, success);
150 if (ret) {
151 goto end;
152 }
153
154 /* Close event element */
155 ret = mi_lttng_writer_close_element(writer);
156end:
157 return ret;
158}
159
e953ef25
DG
160/*
161 * disable_events
162 *
163 * Disabling event using the lttng API.
164 */
cd80958d 165static int disable_events(char *session_name)
e953ef25 166{
e4d484a5
JRJ
167 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
168 int enabled = 1, success = 1;
b73d0b29 169 char *event_name, *channel_name = NULL;
7d29a247 170 struct lttng_domain dom;
6e911cad 171 struct lttng_event event;
e953ef25 172
441c16a7
MD
173 memset(&dom, 0, sizeof(dom));
174
d78d6610 175 /* Create lttng domain */
7d29a247
DG
176 if (opt_kernel) {
177 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 178 } else if (opt_userspace) {
9730260e 179 dom.type = LTTNG_DOMAIN_UST;
b9dfb167
DG
180 } else if (opt_jul) {
181 dom.type = LTTNG_DOMAIN_JUL;
5cdb6027
DG
182 } else if (opt_log4j) {
183 dom.type = LTTNG_DOMAIN_LOG4J;
0e115563
DG
184 } else if (opt_python) {
185 dom.type = LTTNG_DOMAIN_PYTHON;
9730260e 186 } else {
b9dfb167 187 print_missing_domain();
d16c1a4c 188 ret = CMD_ERROR;
9730260e 189 goto error;
7d29a247
DG
190 }
191
85076754 192 channel_name = opt_channel_name;
ae856491 193
cd80958d
DG
194 handle = lttng_create_handle(session_name, &dom);
195 if (handle == NULL) {
196 ret = -1;
197 goto error;
198 }
199
e4d484a5
JRJ
200 /* Mi print the channel and open the events element */
201 if (lttng_opt_mi) {
202 ret = mi_lttng_writer_open_element(writer, config_element_channel);
203 if (ret) {
204 ret = CMD_ERROR;
205 goto end;
e953ef25
DG
206 }
207
e4d484a5
JRJ
208 ret = mi_lttng_writer_write_element_string(writer,
209 config_element_name, print_channel_name(channel_name));
210 if (ret) {
211 ret = CMD_ERROR;
212 goto end;
213 }
214
215 /* Open events element */
216 ret = mi_lttng_writer_open_element(writer, config_element_events);
217 if (ret) {
218 ret = CMD_ERROR;
219 goto end;
220 }
e953ef25
DG
221 }
222
6e911cad
MD
223 memset(&event, 0, sizeof(event));
224 switch (opt_event_type) {
225 case LTTNG_EVENT_SYSCALL:
226 event.type = LTTNG_EVENT_SYSCALL;
227 break;
228 default:
229 event.type = LTTNG_EVENT_ALL;
230 break;
231 }
232
e4d484a5 233 if (opt_disable_all) {
6e911cad 234 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
e4d484a5
JRJ
235 if (command_ret < 0) {
236 ERR("%s", lttng_strerror(command_ret));
237 enabled = 1;
238 success = 0;
239
9730260e 240 } else {
e4d484a5
JRJ
241 enabled = 0;
242 success = 1;
6e911cad
MD
243 MSG("All %s %s are disabled in channel %s",
244 get_domain_str(dom.type),
245 opt_event_type == LTTNG_EVENT_SYSCALL ? "system calls" : "events",
246 print_channel_name(channel_name));
9730260e
DG
247 }
248
e4d484a5
JRJ
249 if (lttng_opt_mi) {
250 ret = mi_print_event("*", enabled, success);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto error;
254 }
255 }
256 } else {
257 /* Strip event list */
258 event_name = strtok(opt_event_list, ",");
259 while (event_name != NULL) {
260 DBG("Disabling event %s", event_name);
e953ef25 261
6e911cad
MD
262 strncpy(event.name, event_name, sizeof(event.name));
263 event.name[sizeof(event.name) - 1] = '\0';
264 command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
e4d484a5 265 if (command_ret < 0) {
6e911cad
MD
266 ERR("%s %s: %s (channel %s, session %s)",
267 opt_event_type == LTTNG_EVENT_SYSCALL ? "System call" : "Event",
268 event_name,
e4d484a5
JRJ
269 lttng_strerror(command_ret),
270 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
271 ? print_raw_channel_name(channel_name)
272 : print_channel_name(channel_name),
273 session_name);
274 warn = 1;
275 success = 0;
276 /*
6e911cad
MD
277 * If an error occurred we assume that the event is still
278 * enabled.
e4d484a5
JRJ
279 */
280 enabled = 1;
281 } else {
6e911cad
MD
282 MSG("%s %s %s disabled in channel %s for session %s",
283 get_domain_str(dom.type),
284 opt_event_type == LTTNG_EVENT_SYSCALL ? "system call" : "event",
285 event_name,
e4d484a5
JRJ
286 print_channel_name(channel_name),
287 session_name);
288 success = 1;
289 enabled = 0;
290 }
291
292 if (lttng_opt_mi) {
293 ret = mi_print_event(event_name, enabled, success);
294 if (ret) {
295 ret = CMD_ERROR;
296 goto error;
297 }
298 }
299
300 /* Next event */
301 event_name = strtok(NULL, ",");
302 }
303 }
ae856491 304
9730260e 305end:
e4d484a5
JRJ
306 if (lttng_opt_mi) {
307 /* Close events element and channel element */
308 ret = mi_lttng_close_multi_element(writer, 2);
309 if (ret) {
310 ret = CMD_ERROR;
311 }
312 }
e953ef25 313error:
e4d484a5
JRJ
314 /* if there is already an error preserve it */
315 if (warn && !ret) {
ae856491
DG
316 ret = CMD_WARNING;
317 }
cd80958d 318
e4d484a5
JRJ
319 /* Overwrite ret if an error occurred */
320 ret = command_ret ? command_ret : ret;
321
322 lttng_destroy_handle(handle);
e953ef25
DG
323 return ret;
324}
325
326/*
327 * cmd_disable_events
328 *
329 * Disable event to trace session
330 */
331int cmd_disable_events(int argc, const char **argv)
332{
e4d484a5 333 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
e953ef25 334 static poptContext pc;
cd80958d 335 char *session_name = NULL;
6e911cad 336 int event_type = -1;
e953ef25
DG
337
338 pc = poptGetContext(NULL, argc, argv, long_options, 0);
339 poptReadDefaultConfig(pc, 0);
340
6e911cad
MD
341 /* Default event type */
342 opt_event_type = LTTNG_EVENT_ALL;
343
e953ef25
DG
344 while ((opt = poptGetNextOpt(pc)) != -1) {
345 switch (opt) {
346 case OPT_HELP:
ca1c3607 347 usage(stdout);
e953ef25
DG
348 goto end;
349 case OPT_USERSPACE:
350 opt_userspace = 1;
e953ef25 351 break;
6e911cad
MD
352 case OPT_SYSCALL:
353 opt_event_type = LTTNG_EVENT_SYSCALL;
354 break;
679b4943
SM
355 case OPT_LIST_OPTIONS:
356 list_cmd_options(stdout, long_options);
679b4943 357 goto end;
e953ef25
DG
358 default:
359 usage(stderr);
360 ret = CMD_UNDEFINED;
361 goto end;
362 }
6e911cad
MD
363
364 /* Validate event type. Multiple event type are not supported. */
365 if (event_type == -1) {
366 event_type = opt_event_type;
367 } else {
368 if (event_type != opt_event_type) {
369 ERR("Multiple event type not supported.");
370 ret = CMD_ERROR;
371 goto end;
372 }
373 }
e953ef25
DG
374 }
375
376 opt_event_list = (char*) poptGetArg(pc);
377 if (opt_event_list == NULL && opt_disable_all == 0) {
378 ERR("Missing event name(s).\n");
379 usage(stderr);
ca1c3607 380 ret = CMD_ERROR;
e953ef25
DG
381 goto end;
382 }
383
cd80958d
DG
384 if (!opt_session_name) {
385 session_name = get_session_name();
386 if (session_name == NULL) {
ca1c3607 387 ret = CMD_ERROR;
cd80958d
DG
388 goto end;
389 }
390 } else {
391 session_name = opt_session_name;
392 }
393
e4d484a5
JRJ
394 /* Mi check */
395 if (lttng_opt_mi) {
396 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
397 if (!writer) {
398 ret = -LTTNG_ERR_NOMEM;
399 goto end;
400 }
401
402 /* Open command element */
403 ret = mi_lttng_writer_command_open(writer,
404 mi_lttng_element_command_disable_event);
405 if (ret) {
406 ret = CMD_ERROR;
407 goto end;
408 }
409
410 /* Open output element */
411 ret = mi_lttng_writer_open_element(writer,
412 mi_lttng_element_command_output);
413 if (ret) {
414 ret = CMD_ERROR;
415 goto end;
416 }
417 }
418
419 command_ret = disable_events(session_name);
420 if (command_ret) {
421 success = 0;
422 }
423
424 /* Mi closing */
425 if (lttng_opt_mi) {
426 /* Close output element */
427 ret = mi_lttng_writer_close_element(writer);
428 if (ret) {
429 ret = CMD_ERROR;
430 goto end;
431 }
432
433 ret = mi_lttng_writer_write_element_bool(writer,
434 mi_lttng_element_command_success, success);
435 if (ret) {
436 ret = CMD_ERROR;
437 goto end;
438 }
439
440 /* Command element close */
441 ret = mi_lttng_writer_command_close(writer);
442 if (ret) {
443 ret = CMD_ERROR;
444 goto end;
445 }
446 }
e953ef25
DG
447
448end:
5853fd43
DG
449 if (!opt_session_name && session_name) {
450 free(session_name);
451 }
e4d484a5
JRJ
452
453 /* Mi clean-up */
454 if (writer && mi_lttng_writer_destroy(writer)) {
455 /* Preserve original error code */
456 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
457 }
458
459 /* Overwrite ret if an error occured in disable_events */
460 ret = command_ret ? command_ret : ret;
461
ca1c3607 462 poptFreeContext(pc);
e953ef25
DG
463 return ret;
464}
This page took 0.056917 seconds and 4 git commands to generate.