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