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