63e5f47418964b876cd2077f6ce1b0833b0aec3c
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
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>
26 #include <assert.h>
27
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32 static char *opt_event_list;
33 static int opt_kernel;
34 static char *opt_channel_name;
35 static char *opt_session_name;
36 static int opt_userspace;
37 static int opt_disable_all;
38 static int opt_jul;
39 #if 0
40 /* Not implemented yet */
41 static char *opt_cmd_name;
42 static pid_t opt_pid;
43 #endif
44
45 enum {
46 OPT_HELP = 1,
47 OPT_USERSPACE,
48 OPT_LIST_OPTIONS,
49 };
50
51 static struct lttng_handle *handle;
52 static struct mi_writer *writer;
53
54 static struct poptOption long_options[] = {
55 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
56 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
57 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
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},
60 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
61 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
62 #if 0
63 /* Not implemented yet */
64 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
65 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
66 #else
67 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
68 #endif
69 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
70 {0, 0, 0, 0, 0, 0, 0}
71 };
72
73 /*
74 * usage
75 */
76 static void usage(FILE *ofp)
77 {
78 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
79 fprintf(ofp, "\n");
80 fprintf(ofp, "Options:\n");
81 fprintf(ofp, " -h, --help Show this help\n");
82 fprintf(ofp, " --list-options Simple listing of options\n");
83 fprintf(ofp, " -s, --session NAME Apply to session name\n");
84 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
85 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
86 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
87 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
88 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
89 fprintf(ofp, "\n");
90 }
91
92 static
93 const char *print_channel_name(const char *name)
94 {
95 return name ? : DEFAULT_CHANNEL_NAME;
96 }
97
98 static
99 const char *print_raw_channel_name(const char *name)
100 {
101 return name ? : "<default>";
102 }
103
104 /* Mi print a partial event.
105 * enabled is 0 or 1
106 * success is 0 or 1
107 */
108 static 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);
144 end:
145 return ret;
146 }
147
148 /*
149 * disable_events
150 *
151 * Disabling event using the lttng API.
152 */
153 static int disable_events(char *session_name)
154 {
155 int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
156 int enabled = 1, success = 1;
157 char *event_name, *channel_name = NULL;
158 struct lttng_domain dom;
159
160 memset(&dom, 0, sizeof(dom));
161
162 /* Create lttng domain */
163 if (opt_kernel) {
164 dom.type = LTTNG_DOMAIN_KERNEL;
165 } else if (opt_userspace) {
166 dom.type = LTTNG_DOMAIN_UST;
167 } else if (opt_jul) {
168 dom.type = LTTNG_DOMAIN_JUL;
169 } else {
170 print_missing_domain();
171 ret = CMD_ERROR;
172 goto error;
173 }
174
175 channel_name = opt_channel_name;
176
177 handle = lttng_create_handle(session_name, &dom);
178 if (handle == NULL) {
179 ret = -1;
180 goto error;
181 }
182
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;
189 }
190
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 }
204 }
205
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
213 } else {
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));
218 }
219
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);
232
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 }
269
270 end:
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 }
278 error:
279 /* if there is already an error preserve it */
280 if (warn && !ret) {
281 ret = CMD_WARNING;
282 }
283
284 /* Overwrite ret if an error occurred */
285 ret = command_ret ? command_ret : ret;
286
287 lttng_destroy_handle(handle);
288 return ret;
289 }
290
291 /*
292 * cmd_disable_events
293 *
294 * Disable event to trace session
295 */
296 int cmd_disable_events(int argc, const char **argv)
297 {
298 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
299 static poptContext pc;
300 char *session_name = NULL;
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:
308 usage(stdout);
309 goto end;
310 case OPT_USERSPACE:
311 opt_userspace = 1;
312 break;
313 case OPT_LIST_OPTIONS:
314 list_cmd_options(stdout, long_options);
315 goto end;
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);
327 ret = CMD_ERROR;
328 goto end;
329 }
330
331 if (!opt_session_name) {
332 session_name = get_session_name();
333 if (session_name == NULL) {
334 ret = CMD_ERROR;
335 goto end;
336 }
337 } else {
338 session_name = opt_session_name;
339 }
340
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 }
394
395 end:
396 if (!opt_session_name && session_name) {
397 free(session_name);
398 }
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
409 poptFreeContext(pc);
410 return ret;
411 }
This page took 0.036345 seconds and 3 git commands to generate.