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