Mi start command: support and validation
[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
27 #include "../command.h"
28
29 static char *opt_event_list;
30 static int opt_kernel;
31 static char *opt_channel_name;
32 static char *opt_session_name;
33 static int opt_userspace;
34 static int opt_disable_all;
35 static int opt_jul;
36 #if 0
37 /* Not implemented yet */
38 static char *opt_cmd_name;
39 static pid_t opt_pid;
40 #endif
41
42 enum {
43 OPT_HELP = 1,
44 OPT_USERSPACE,
45 OPT_LIST_OPTIONS,
46 };
47
48 static struct lttng_handle *handle;
49
50 static struct poptOption long_options[] = {
51 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
52 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
53 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
54 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
55 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
56 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
57 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
58 #if 0
59 /* Not implemented yet */
60 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
61 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
62 #else
63 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
64 #endif
65 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
66 {0, 0, 0, 0, 0, 0, 0}
67 };
68
69 /*
70 * usage
71 */
72 static void usage(FILE *ofp)
73 {
74 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, "Options:\n");
77 fprintf(ofp, " -h, --help Show this help\n");
78 fprintf(ofp, " --list-options Simple listing of options\n");
79 fprintf(ofp, " -s, --session NAME Apply to session name\n");
80 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
81 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
82 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
83 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
84 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
85 fprintf(ofp, "\n");
86 }
87
88 static
89 const char *print_channel_name(const char *name)
90 {
91 return name ? : DEFAULT_CHANNEL_NAME;
92 }
93
94 static
95 const char *print_raw_channel_name(const char *name)
96 {
97 return name ? : "<default>";
98 }
99
100 /*
101 * disable_events
102 *
103 * Disabling event using the lttng API.
104 */
105 static int disable_events(char *session_name)
106 {
107 int ret = CMD_SUCCESS, warn = 0;
108 char *event_name, *channel_name = NULL;
109 struct lttng_domain dom;
110
111 memset(&dom, 0, sizeof(dom));
112
113 /* Create lttng domain */
114 if (opt_kernel) {
115 dom.type = LTTNG_DOMAIN_KERNEL;
116 } else if (opt_userspace) {
117 dom.type = LTTNG_DOMAIN_UST;
118 } else if (opt_jul) {
119 dom.type = LTTNG_DOMAIN_JUL;
120 } else {
121 print_missing_domain();
122 ret = CMD_ERROR;
123 goto error;
124 }
125
126 channel_name = opt_channel_name;
127
128 handle = lttng_create_handle(session_name, &dom);
129 if (handle == NULL) {
130 ret = -1;
131 goto error;
132 }
133
134 if (opt_disable_all) {
135 ret = lttng_disable_event(handle, NULL, channel_name);
136 if (ret < 0) {
137 ERR("%s", lttng_strerror(ret));
138 goto error;
139 }
140
141 MSG("All %s events are disabled in channel %s",
142 get_domain_str(dom.type), print_channel_name(channel_name));
143 goto end;
144 }
145
146 /* Strip event list */
147 event_name = strtok(opt_event_list, ",");
148 while (event_name != NULL) {
149 DBG("Disabling event %s", event_name);
150
151 ret = lttng_disable_event(handle, event_name, channel_name);
152 if (ret < 0) {
153 ERR("Event %s: %s (channel %s, session %s)", event_name,
154 lttng_strerror(ret),
155 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
156 ? print_raw_channel_name(channel_name)
157 : print_channel_name(channel_name),
158 session_name);
159 warn = 1;
160 } else {
161 MSG("%s event %s disabled in channel %s for session %s",
162 get_domain_str(dom.type), event_name,
163 print_channel_name(channel_name),
164 session_name);
165 }
166
167 /* Next event */
168 event_name = strtok(NULL, ",");
169 }
170
171 ret = CMD_SUCCESS;
172
173 end:
174 error:
175 if (warn) {
176 ret = CMD_WARNING;
177 }
178 lttng_destroy_handle(handle);
179
180 return ret;
181 }
182
183 /*
184 * cmd_disable_events
185 *
186 * Disable event to trace session
187 */
188 int cmd_disable_events(int argc, const char **argv)
189 {
190 int opt, ret = CMD_SUCCESS;
191 static poptContext pc;
192 char *session_name = NULL;
193
194 pc = poptGetContext(NULL, argc, argv, long_options, 0);
195 poptReadDefaultConfig(pc, 0);
196
197 while ((opt = poptGetNextOpt(pc)) != -1) {
198 switch (opt) {
199 case OPT_HELP:
200 usage(stdout);
201 goto end;
202 case OPT_USERSPACE:
203 opt_userspace = 1;
204 break;
205 case OPT_LIST_OPTIONS:
206 list_cmd_options(stdout, long_options);
207 goto end;
208 default:
209 usage(stderr);
210 ret = CMD_UNDEFINED;
211 goto end;
212 }
213 }
214
215 /* TODO: mi support */
216 if (lttng_opt_mi) {
217 ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
218 ERR("mi option not supported");
219 goto end;
220 }
221
222 opt_event_list = (char*) poptGetArg(pc);
223 if (opt_event_list == NULL && opt_disable_all == 0) {
224 ERR("Missing event name(s).\n");
225 usage(stderr);
226 ret = CMD_ERROR;
227 goto end;
228 }
229
230 if (!opt_session_name) {
231 session_name = get_session_name();
232 if (session_name == NULL) {
233 ret = CMD_ERROR;
234 goto end;
235 }
236 } else {
237 session_name = opt_session_name;
238 }
239
240 ret = disable_events(session_name);
241
242 end:
243 if (!opt_session_name && session_name) {
244 free(session_name);
245 }
246 poptFreeContext(pc);
247 return ret;
248 }
This page took 0.035222 seconds and 4 git commands to generate.