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