Initialize all stack variables to zero, fix uninitialized loglevel variables
[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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "../command.h"
29
30 static char *opt_event_list;
31 static int opt_kernel;
32 static char *opt_channel_name;
33 static char *opt_session_name;
34 static int opt_userspace;
35 static int opt_disable_all;
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 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
57 #if 0
58 /* Not implemented yet */
59 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
60 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
61 #else
62 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
63 #endif
64 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
65 {0, 0, 0, 0, 0, 0, 0}
66 };
67
68 /*
69 * usage
70 */
71 static void usage(FILE *ofp)
72 {
73 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n");
74 fprintf(ofp, "\n");
75 fprintf(ofp, " -h, --help Show this help\n");
76 fprintf(ofp, " --list-options Simple listing of options\n");
77 fprintf(ofp, " -s, --session Apply to session name\n");
78 fprintf(ofp, " -c, --channel Apply to this channel\n");
79 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
80 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
81 #if 0
82 fprintf(ofp, " -u, --userspace [CMD] Apply to the user-space tracer\n");
83 fprintf(ofp, " If no CMD, the domain used is UST global\n");
84 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
85 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
86 #else
87 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
88 #endif
89 fprintf(ofp, "\n");
90 }
91
92 /*
93 * disable_events
94 *
95 * Disabling event using the lttng API.
96 */
97 static int disable_events(char *session_name)
98 {
99 int err, ret = CMD_SUCCESS, warn = 0;
100 char *event_name, *channel_name = NULL;
101 struct lttng_domain dom;
102
103 memset(&dom, 0, sizeof(dom));
104
105 /* Create lttng domain */
106 if (opt_kernel) {
107 dom.type = LTTNG_DOMAIN_KERNEL;
108 } else if (opt_userspace) {
109 dom.type = LTTNG_DOMAIN_UST;
110 } else {
111 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
112 ret = CMD_ERROR;
113 goto error;
114 }
115
116 /* Get channel name */
117 if (opt_channel_name == NULL) {
118 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
119 if (err < 0) {
120 ret = CMD_FATAL;
121 goto error;
122 }
123 } else {
124 channel_name = opt_channel_name;
125 }
126
127 handle = lttng_create_handle(session_name, &dom);
128 if (handle == NULL) {
129 ret = -1;
130 goto error;
131 }
132
133 if (opt_disable_all) {
134 ret = lttng_disable_event(handle, NULL, channel_name);
135 if (ret < 0) {
136 /* Don't set ret so lttng can interpret the sessiond error. */
137 goto error;
138 }
139
140 MSG("All %s events are disabled in channel %s",
141 opt_kernel ? "kernel" : "UST", channel_name);
142 goto end;
143 }
144
145 /* Strip event list */
146 event_name = strtok(opt_event_list, ",");
147 while (event_name != NULL) {
148 DBG("Disabling event %s", event_name);
149
150 ret = lttng_disable_event(handle, event_name, channel_name);
151 if (ret < 0) {
152 ERR("Event %s: %s (channel %s, session %s)", event_name,
153 lttng_strerror(ret), channel_name, session_name);
154 warn = 1;
155 } else {
156 MSG("%s event %s disabled in channel %s for session %s",
157 opt_kernel ? "kernel" : "UST", event_name, channel_name,
158 session_name);
159 }
160
161 /* Next event */
162 event_name = strtok(NULL, ",");
163 }
164
165 ret = CMD_SUCCESS;
166
167 end:
168 error:
169 if (warn) {
170 ret = CMD_WARNING;
171 }
172 if (opt_channel_name == NULL) {
173 free(channel_name);
174 }
175 lttng_destroy_handle(handle);
176
177 return ret;
178 }
179
180 /*
181 * cmd_disable_events
182 *
183 * Disable event to trace session
184 */
185 int cmd_disable_events(int argc, const char **argv)
186 {
187 int opt, ret = CMD_SUCCESS;
188 static poptContext pc;
189 char *session_name = NULL;
190
191 pc = poptGetContext(NULL, argc, argv, long_options, 0);
192 poptReadDefaultConfig(pc, 0);
193
194 while ((opt = poptGetNextOpt(pc)) != -1) {
195 switch (opt) {
196 case OPT_HELP:
197 usage(stdout);
198 goto end;
199 case OPT_USERSPACE:
200 opt_userspace = 1;
201 break;
202 case OPT_LIST_OPTIONS:
203 list_cmd_options(stdout, long_options);
204 goto end;
205 default:
206 usage(stderr);
207 ret = CMD_UNDEFINED;
208 goto end;
209 }
210 }
211
212 opt_event_list = (char*) poptGetArg(pc);
213 if (opt_event_list == NULL && opt_disable_all == 0) {
214 ERR("Missing event name(s).\n");
215 usage(stderr);
216 ret = CMD_ERROR;
217 goto end;
218 }
219
220 if (!opt_session_name) {
221 session_name = get_session_name();
222 if (session_name == NULL) {
223 ret = CMD_ERROR;
224 goto end;
225 }
226 } else {
227 session_name = opt_session_name;
228 }
229
230 ret = disable_events(session_name);
231
232 end:
233 if (!opt_session_name && session_name) {
234 free(session_name);
235 }
236 poptFreeContext(pc);
237 return ret;
238 }
This page took 0.033648 seconds and 4 git commands to generate.