Fix: check malloc return value
[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 #if 0
36 /* Not implemented yet */
37 static char *opt_cmd_name;
38 static pid_t opt_pid;
39 #endif
40
41 enum {
42 OPT_HELP = 1,
43 OPT_USERSPACE,
44 OPT_LIST_OPTIONS,
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 #if 0
57 /* Not implemented yet */
58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
60 #else
61 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
62 #endif
63 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
64 {0, 0, 0, 0, 0, 0, 0}
65 };
66
67 /*
68 * usage
69 */
70 static void usage(FILE *ofp)
71 {
72 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
73 fprintf(ofp, "\n");
74 fprintf(ofp, "Options:\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 NAME Apply to session name\n");
78 fprintf(ofp, " -c, --channel NAME 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 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
82 fprintf(ofp, "\n");
83 }
84
85 static
86 const char *print_channel_name(const char *name)
87 {
88 return name ? : DEFAULT_CHANNEL_NAME;
89 }
90
91 static
92 const char *print_raw_channel_name(const char *name)
93 {
94 return name ? : "<default>";
95 }
96
97 /*
98 * disable_events
99 *
100 * Disabling event using the lttng API.
101 */
102 static int disable_events(char *session_name)
103 {
104 int ret = CMD_SUCCESS, warn = 0;
105 char *event_name, *channel_name = NULL;
106 struct lttng_domain dom;
107
108 memset(&dom, 0, sizeof(dom));
109
110 /* Create lttng domain */
111 if (opt_kernel) {
112 dom.type = LTTNG_DOMAIN_KERNEL;
113 } else if (opt_userspace) {
114 dom.type = LTTNG_DOMAIN_UST;
115 } else {
116 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
117 ret = CMD_ERROR;
118 goto error;
119 }
120
121 channel_name = opt_channel_name;
122
123 handle = lttng_create_handle(session_name, &dom);
124 if (handle == NULL) {
125 ret = -1;
126 goto error;
127 }
128
129 if (opt_disable_all) {
130 ret = lttng_disable_event(handle, NULL, channel_name);
131 if (ret < 0) {
132 ERR("%s", lttng_strerror(ret));
133 goto error;
134 }
135
136 MSG("All %s events are disabled in channel %s",
137 opt_kernel ? "kernel" : "UST",
138 print_channel_name(channel_name));
139 goto end;
140 }
141
142 /* Strip event list */
143 event_name = strtok(opt_event_list, ",");
144 while (event_name != NULL) {
145 DBG("Disabling event %s", event_name);
146
147 ret = lttng_disable_event(handle, event_name, channel_name);
148 if (ret < 0) {
149 ERR("Event %s: %s (channel %s, session %s)", event_name,
150 lttng_strerror(ret),
151 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
152 ? print_raw_channel_name(channel_name)
153 : print_channel_name(channel_name),
154 session_name);
155 warn = 1;
156 } else {
157 MSG("%s event %s disabled in channel %s for session %s",
158 opt_kernel ? "kernel" : "UST", event_name,
159 print_channel_name(channel_name),
160 session_name);
161 }
162
163 /* Next event */
164 event_name = strtok(NULL, ",");
165 }
166
167 ret = CMD_SUCCESS;
168
169 end:
170 error:
171 if (warn) {
172 ret = CMD_WARNING;
173 }
174 lttng_destroy_handle(handle);
175
176 return ret;
177 }
178
179 /*
180 * cmd_disable_events
181 *
182 * Disable event to trace session
183 */
184 int cmd_disable_events(int argc, const char **argv)
185 {
186 int opt, ret = CMD_SUCCESS;
187 static poptContext pc;
188 char *session_name = NULL;
189
190 pc = poptGetContext(NULL, argc, argv, long_options, 0);
191 poptReadDefaultConfig(pc, 0);
192
193 while ((opt = poptGetNextOpt(pc)) != -1) {
194 switch (opt) {
195 case OPT_HELP:
196 usage(stdout);
197 goto end;
198 case OPT_USERSPACE:
199 opt_userspace = 1;
200 break;
201 case OPT_LIST_OPTIONS:
202 list_cmd_options(stdout, long_options);
203 goto end;
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_ERROR;
216 goto end;
217 }
218
219 if (!opt_session_name) {
220 session_name = get_session_name();
221 if (session_name == NULL) {
222 ret = CMD_ERROR;
223 goto end;
224 }
225 } else {
226 session_name = opt_session_name;
227 }
228
229 ret = disable_events(session_name);
230
231 end:
232 if (!opt_session_name && session_name) {
233 free(session_name);
234 }
235 poptFreeContext(pc);
236 return ret;
237 }
This page took 0.035898 seconds and 4 git commands to generate.