Update version to 2.0-pre17
[lttng-tools.git] / 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 "../cmd.h"
29 #include "../conf.h"
30 #include "../utils.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 char *opt_cmd_name;
38 static int opt_disable_all;
39 static pid_t opt_pid;
40
41 enum {
42 OPT_HELP = 1,
43 OPT_USERSPACE,
44 };
45
46 static struct lttng_handle *handle;
47
48 static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
51 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
52 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
53 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
54 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
55 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
56 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
57 {0, 0, 0, 0, 0, 0, 0}
58 };
59
60 /*
61 * usage
62 */
63 static void usage(FILE *ofp)
64 {
65 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n");
66 fprintf(ofp, "\n");
67 fprintf(ofp, " -h, --help Show this help\n");
68 fprintf(ofp, " -s, --session Apply on session name\n");
69 fprintf(ofp, " -c, --channel Apply on this channel\n");
70 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
71 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
72 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
73 fprintf(ofp, " If no CMD, the domain used is UST global\n");
74 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
75 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
76 fprintf(ofp, "\n");
77 }
78
79 /*
80 * disable_events
81 *
82 * Disabling event using the lttng API.
83 */
84 static int disable_events(char *session_name)
85 {
86 int err, ret = CMD_SUCCESS;
87 char *event_name, *channel_name = NULL;
88 struct lttng_domain dom;
89
90 if (opt_channel_name == NULL) {
91 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
92 if (err < 0) {
93 ret = CMD_FATAL;
94 goto error;
95 }
96 } else {
97 channel_name = opt_channel_name;
98 }
99
100 if (opt_kernel && opt_userspace) {
101 ERR("Can't use -k/--kernel and -u/--userspace together");
102 ret = CMD_FATAL;
103 goto error;
104 }
105
106 if (opt_kernel) {
107 dom.type = LTTNG_DOMAIN_KERNEL;
108 } else if (opt_pid != 0) {
109 dom.type = LTTNG_DOMAIN_UST_PID;
110 dom.attr.pid = opt_pid;
111 DBG("PID %d set to lttng handle", opt_pid);
112 } else if (opt_userspace && opt_cmd_name == NULL) {
113 dom.type = LTTNG_DOMAIN_UST;
114 DBG("UST global domain selected");
115 } else if (opt_userspace && opt_cmd_name != NULL) {
116 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
117 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
118 dom.attr.exec_name[NAME_MAX - 1] = '\0';
119 } else {
120 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
121 ret = CMD_NOT_IMPLEMENTED;
122 goto error;
123 }
124
125 handle = lttng_create_handle(session_name, &dom);
126 if (handle == NULL) {
127 ret = -1;
128 goto error;
129 }
130
131 if (opt_disable_all) {
132 ret = lttng_disable_event(handle, NULL, channel_name);
133 if (ret < 0) {
134 goto error;
135 }
136
137 MSG("All %s events are disabled in channel %s",
138 opt_kernel ? "kernel" : "UST", channel_name);
139 goto end;
140 }
141
142 /* Strip event list */
143 event_name = strtok(opt_event_list, ",");
144 while (event_name != NULL) {
145 /* Kernel tracer action */
146 if (opt_kernel) {
147 DBG("Disabling kernel event %s in channel %s",
148 event_name, channel_name);
149 } else if (opt_userspace) { /* User-space tracer action */
150 if (opt_cmd_name != NULL || opt_pid) {
151 MSG("Only supporting tracing all UST processes (-u) for now.");
152 ret = CMD_NOT_IMPLEMENTED;
153 goto error;
154 }
155 DBG("Disabling UST event %s in channel %s",
156 event_name, channel_name);
157 } else {
158 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
159 goto error;
160 }
161
162 ret = lttng_disable_event(handle, event_name, channel_name);
163 if (ret < 0) {
164 MSG("Unable to disable %s event %s in channel %s",
165 opt_kernel ? "kernel" : "UST", event_name,
166 channel_name);
167 } else {
168 MSG("%s event %s disabled in channel %s",
169 opt_kernel ? "kernel" : "UST", event_name,
170 channel_name);
171 }
172
173 /* Next event */
174 event_name = strtok(NULL, ",");
175 }
176
177 end:
178 error:
179 if (opt_channel_name == NULL) {
180 free(channel_name);
181 }
182 lttng_destroy_handle(handle);
183
184 return ret;
185 }
186
187 /*
188 * cmd_disable_events
189 *
190 * Disable event to trace session
191 */
192 int cmd_disable_events(int argc, const char **argv)
193 {
194 int opt, ret;
195 static poptContext pc;
196 char *session_name = NULL;
197
198 pc = poptGetContext(NULL, argc, argv, long_options, 0);
199 poptReadDefaultConfig(pc, 0);
200
201 while ((opt = poptGetNextOpt(pc)) != -1) {
202 switch (opt) {
203 case OPT_HELP:
204 usage(stderr);
205 ret = CMD_SUCCESS;
206 goto end;
207 case OPT_USERSPACE:
208 opt_userspace = 1;
209 break;
210 default:
211 usage(stderr);
212 ret = CMD_UNDEFINED;
213 goto end;
214 }
215 }
216
217 opt_event_list = (char*) poptGetArg(pc);
218 if (opt_event_list == NULL && opt_disable_all == 0) {
219 ERR("Missing event name(s).\n");
220 usage(stderr);
221 ret = CMD_SUCCESS;
222 goto end;
223 }
224
225 if (!opt_session_name) {
226 session_name = get_session_name();
227 if (session_name == NULL) {
228 ret = -1;
229 goto end;
230 }
231 } else {
232 session_name = opt_session_name;
233 }
234
235 ret = disable_events(session_name);
236
237 end:
238 return ret;
239 }
This page took 0.036196 seconds and 5 git commands to generate.