Mi disable-event command: support and validation
[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;
d78d6610
DG
39#if 0
40/* Not implemented yet */
41static char *opt_cmd_name;
e953ef25 42static pid_t opt_pid;
d78d6610 43#endif
e953ef25
DG
44
45enum {
46 OPT_HELP = 1,
47 OPT_USERSPACE,
679b4943 48 OPT_LIST_OPTIONS,
e953ef25
DG
49};
50
cd80958d 51static struct lttng_handle *handle;
e4d484a5 52static struct mi_writer *writer;
cd80958d 53
e953ef25
DG
54static struct poptOption long_options[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 57 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e953ef25
DG
58 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
59 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
b9dfb167 60 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
e953ef25 61 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
62#if 0
63 /* Not implemented yet */
6caa2bcc 64 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
e953ef25 65 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
66#else
67 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
68#endif
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
73/*
74 * usage
75 */
76static void usage(FILE *ofp)
77{
c138a39b 78 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
e953ef25 79 fprintf(ofp, "\n");
32a6298d 80 fprintf(ofp, "Options:\n");
e953ef25 81 fprintf(ofp, " -h, --help Show this help\n");
679b4943 82 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d
DG
83 fprintf(ofp, " -s, --session NAME Apply to session name\n");
84 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
9730260e 85 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
e953ef25 86 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
27221701 87 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
b9dfb167 88 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
e953ef25
DG
89 fprintf(ofp, "\n");
90}
91
85076754
MD
92static
93const char *print_channel_name(const char *name)
94{
95 return name ? : DEFAULT_CHANNEL_NAME;
96}
97
98static
99const char *print_raw_channel_name(const char *name)
100{
101 return name ? : "<default>";
102}
103
e4d484a5
JRJ
104/* Mi print a partial event.
105 * enabled is 0 or 1
106 * success is 0 or 1
107 */
108static int mi_print_event(char *event_name, int enabled, int success)
109{
110 int ret;
111
112 assert(writer);
113 assert(event_name);
114
115 /* Open event element */
116 ret = mi_lttng_writer_open_element(writer, config_element_event);
117 if (ret) {
118 goto end;
119 }
120
121 /* Print the name of event */
122 ret = mi_lttng_writer_write_element_string(writer,
123 config_element_name, event_name);
124 if (ret) {
125 goto end;
126 }
127
128 /* Print enabled ? */
129 ret = mi_lttng_writer_write_element_bool(writer,
130 config_element_enabled, enabled);
131 if (ret) {
132 goto end;
133 }
134
135 /* Success ? */
136 ret = mi_lttng_writer_write_element_bool(writer,
137 mi_lttng_element_command_success, success);
138 if (ret) {
139 goto end;
140 }
141
142 /* Close event element */
143 ret = mi_lttng_writer_close_element(writer);
144end:
145 return ret;
146}
147
e953ef25
DG
148/*
149 * disable_events
150 *
151 * Disabling event using the lttng API.
152 */
cd80958d 153static int disable_events(char *session_name)
e953ef25 154{
e4d484a5
JRJ
155 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
156 int enabled = 1, success = 1;
b73d0b29 157 char *event_name, *channel_name = NULL;
7d29a247 158 struct lttng_domain dom;
e953ef25 159
441c16a7
MD
160 memset(&dom, 0, sizeof(dom));
161
d78d6610 162 /* Create lttng domain */
7d29a247
DG
163 if (opt_kernel) {
164 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 165 } else if (opt_userspace) {
9730260e 166 dom.type = LTTNG_DOMAIN_UST;
b9dfb167
DG
167 } else if (opt_jul) {
168 dom.type = LTTNG_DOMAIN_JUL;
9730260e 169 } else {
b9dfb167 170 print_missing_domain();
d16c1a4c 171 ret = CMD_ERROR;
9730260e 172 goto error;
7d29a247
DG
173 }
174
85076754 175 channel_name = opt_channel_name;
ae856491 176
cd80958d
DG
177 handle = lttng_create_handle(session_name, &dom);
178 if (handle == NULL) {
179 ret = -1;
180 goto error;
181 }
182
e4d484a5
JRJ
183 /* Mi print the channel and open the events element */
184 if (lttng_opt_mi) {
185 ret = mi_lttng_writer_open_element(writer, config_element_channel);
186 if (ret) {
187 ret = CMD_ERROR;
188 goto end;
e953ef25
DG
189 }
190
e4d484a5
JRJ
191 ret = mi_lttng_writer_write_element_string(writer,
192 config_element_name, print_channel_name(channel_name));
193 if (ret) {
194 ret = CMD_ERROR;
195 goto end;
196 }
197
198 /* Open events element */
199 ret = mi_lttng_writer_open_element(writer, config_element_events);
200 if (ret) {
201 ret = CMD_ERROR;
202 goto end;
203 }
e953ef25
DG
204 }
205
e4d484a5
JRJ
206 if (opt_disable_all) {
207 command_ret = lttng_disable_event(handle, NULL, channel_name);
208 if (command_ret < 0) {
209 ERR("%s", lttng_strerror(command_ret));
210 enabled = 1;
211 success = 0;
212
9730260e 213 } else {
e4d484a5
JRJ
214 enabled = 0;
215 success = 1;
216 MSG("All %s events are disabled in channel %s",
217 get_domain_str(dom.type), print_channel_name(channel_name));
9730260e
DG
218 }
219
e4d484a5
JRJ
220 if (lttng_opt_mi) {
221 ret = mi_print_event("*", enabled, success);
222 if (ret) {
223 ret = CMD_ERROR;
224 goto error;
225 }
226 }
227 } else {
228 /* Strip event list */
229 event_name = strtok(opt_event_list, ",");
230 while (event_name != NULL) {
231 DBG("Disabling event %s", event_name);
e953ef25 232
e4d484a5
JRJ
233 command_ret = lttng_disable_event(handle, event_name, channel_name);
234 if (command_ret < 0) {
235 ERR("Event %s: %s (channel %s, session %s)", event_name,
236 lttng_strerror(command_ret),
237 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
238 ? print_raw_channel_name(channel_name)
239 : print_channel_name(channel_name),
240 session_name);
241 warn = 1;
242 success = 0;
243 /*
244 * If an error occurred we assume that
245 * the event is still enabled.
246 */
247 enabled = 1;
248 } else {
249 MSG("%s event %s disabled in channel %s for session %s",
250 get_domain_str(dom.type), event_name,
251 print_channel_name(channel_name),
252 session_name);
253 success = 1;
254 enabled = 0;
255 }
256
257 if (lttng_opt_mi) {
258 ret = mi_print_event(event_name, enabled, success);
259 if (ret) {
260 ret = CMD_ERROR;
261 goto error;
262 }
263 }
264
265 /* Next event */
266 event_name = strtok(NULL, ",");
267 }
268 }
ae856491 269
9730260e 270end:
e4d484a5
JRJ
271 if (lttng_opt_mi) {
272 /* Close events element and channel element */
273 ret = mi_lttng_close_multi_element(writer, 2);
274 if (ret) {
275 ret = CMD_ERROR;
276 }
277 }
e953ef25 278error:
e4d484a5
JRJ
279 /* if there is already an error preserve it */
280 if (warn && !ret) {
ae856491
DG
281 ret = CMD_WARNING;
282 }
cd80958d 283
e4d484a5
JRJ
284 /* Overwrite ret if an error occurred */
285 ret = command_ret ? command_ret : ret;
286
287 lttng_destroy_handle(handle);
e953ef25
DG
288 return ret;
289}
290
291/*
292 * cmd_disable_events
293 *
294 * Disable event to trace session
295 */
296int cmd_disable_events(int argc, const char **argv)
297{
e4d484a5 298 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
e953ef25 299 static poptContext pc;
cd80958d 300 char *session_name = NULL;
e953ef25
DG
301
302 pc = poptGetContext(NULL, argc, argv, long_options, 0);
303 poptReadDefaultConfig(pc, 0);
304
305 while ((opt = poptGetNextOpt(pc)) != -1) {
306 switch (opt) {
307 case OPT_HELP:
ca1c3607 308 usage(stdout);
e953ef25
DG
309 goto end;
310 case OPT_USERSPACE:
311 opt_userspace = 1;
e953ef25 312 break;
679b4943
SM
313 case OPT_LIST_OPTIONS:
314 list_cmd_options(stdout, long_options);
679b4943 315 goto end;
e953ef25
DG
316 default:
317 usage(stderr);
318 ret = CMD_UNDEFINED;
319 goto end;
320 }
321 }
322
323 opt_event_list = (char*) poptGetArg(pc);
324 if (opt_event_list == NULL && opt_disable_all == 0) {
325 ERR("Missing event name(s).\n");
326 usage(stderr);
ca1c3607 327 ret = CMD_ERROR;
e953ef25
DG
328 goto end;
329 }
330
cd80958d
DG
331 if (!opt_session_name) {
332 session_name = get_session_name();
333 if (session_name == NULL) {
ca1c3607 334 ret = CMD_ERROR;
cd80958d
DG
335 goto end;
336 }
337 } else {
338 session_name = opt_session_name;
339 }
340
e4d484a5
JRJ
341 /* Mi check */
342 if (lttng_opt_mi) {
343 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
344 if (!writer) {
345 ret = -LTTNG_ERR_NOMEM;
346 goto end;
347 }
348
349 /* Open command element */
350 ret = mi_lttng_writer_command_open(writer,
351 mi_lttng_element_command_disable_event);
352 if (ret) {
353 ret = CMD_ERROR;
354 goto end;
355 }
356
357 /* Open output element */
358 ret = mi_lttng_writer_open_element(writer,
359 mi_lttng_element_command_output);
360 if (ret) {
361 ret = CMD_ERROR;
362 goto end;
363 }
364 }
365
366 command_ret = disable_events(session_name);
367 if (command_ret) {
368 success = 0;
369 }
370
371 /* Mi closing */
372 if (lttng_opt_mi) {
373 /* Close output element */
374 ret = mi_lttng_writer_close_element(writer);
375 if (ret) {
376 ret = CMD_ERROR;
377 goto end;
378 }
379
380 ret = mi_lttng_writer_write_element_bool(writer,
381 mi_lttng_element_command_success, success);
382 if (ret) {
383 ret = CMD_ERROR;
384 goto end;
385 }
386
387 /* Command element close */
388 ret = mi_lttng_writer_command_close(writer);
389 if (ret) {
390 ret = CMD_ERROR;
391 goto end;
392 }
393 }
e953ef25
DG
394
395end:
5853fd43
DG
396 if (!opt_session_name && session_name) {
397 free(session_name);
398 }
e4d484a5
JRJ
399
400 /* Mi clean-up */
401 if (writer && mi_lttng_writer_destroy(writer)) {
402 /* Preserve original error code */
403 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
404 }
405
406 /* Overwrite ret if an error occured in disable_events */
407 ret = command_ret ? command_ret : ret;
408
ca1c3607 409 poptFreeContext(pc);
e953ef25
DG
410 return ret;
411}
This page took 0.053899 seconds and 4 git commands to generate.