Fix: check malloc return value
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.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_channels;
30 static int opt_kernel;
31 static char *opt_session_name;
32 static int opt_userspace;
33 #if 0
34 /* Not implemented yet */
35 static char *opt_cmd_name;
36 static pid_t opt_pid;
37 #endif
38
39 enum {
40 OPT_HELP = 1,
41 OPT_USERSPACE,
42 OPT_LIST_OPTIONS,
43 };
44
45 static struct lttng_handle *handle;
46
47 static struct poptOption long_options[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
50 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
51 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
52 #if 0
53 /* Not implemented yet */
54 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
55 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
56 #else
57 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
58 #endif
59 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
60 {0, 0, 0, 0, 0, 0, 0}
61 };
62
63 /*
64 * usage
65 */
66 static void usage(FILE *ofp)
67 {
68 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
69 fprintf(ofp, "\n");
70 fprintf(ofp, "Options:\n");
71 fprintf(ofp, " -h, --help Show this help\n");
72 fprintf(ofp, " --list-options Simple listing of options\n");
73 fprintf(ofp, " -s, --session NAME Apply to session name\n");
74 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
75 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
76 fprintf(ofp, "\n");
77 }
78
79 /*
80 * Disabling channel using the lttng API.
81 */
82 static int disable_channels(char *session_name)
83 {
84 int ret = CMD_SUCCESS, warn = 0;
85 char *channel_name;
86 struct lttng_domain dom;
87
88 memset(&dom, 0, sizeof(dom));
89
90 /* Create lttng domain */
91 if (opt_kernel) {
92 dom.type = LTTNG_DOMAIN_KERNEL;
93 } else if (opt_userspace) {
94 dom.type = LTTNG_DOMAIN_UST;
95 } else {
96 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
97 ret = CMD_ERROR;
98 goto error;
99 }
100
101 handle = lttng_create_handle(session_name, &dom);
102 if (handle == NULL) {
103 ret = -1;
104 goto error;
105 }
106
107 /* Strip channel list */
108 channel_name = strtok(opt_channels, ",");
109 while (channel_name != NULL) {
110 DBG("Disabling channel %s", channel_name);
111
112 ret = lttng_disable_channel(handle, channel_name);
113 if (ret < 0) {
114 ERR("Channel %s: %s (session %s)", channel_name,
115 lttng_strerror(ret), session_name);
116 warn = 1;
117 } else {
118 MSG("%s channel %s disabled for session %s",
119 opt_kernel ? "Kernel" : "UST", channel_name, session_name);
120 }
121
122 /* Next channel */
123 channel_name = strtok(NULL, ",");
124 }
125
126 ret = CMD_SUCCESS;
127
128 error:
129 if (warn) {
130 ret = CMD_WARNING;
131 }
132
133 lttng_destroy_handle(handle);
134
135 return ret;
136 }
137
138 /*
139 * cmd_disable_channels
140 *
141 * Disable channel to trace session
142 */
143 int cmd_disable_channels(int argc, const char **argv)
144 {
145 int opt, ret = CMD_SUCCESS;
146 static poptContext pc;
147 char *session_name = NULL;
148
149 pc = poptGetContext(NULL, argc, argv, long_options, 0);
150 poptReadDefaultConfig(pc, 0);
151
152 while ((opt = poptGetNextOpt(pc)) != -1) {
153 switch (opt) {
154 case OPT_HELP:
155 usage(stdout);
156 goto end;
157 case OPT_USERSPACE:
158 opt_userspace = 1;
159 break;
160 case OPT_LIST_OPTIONS:
161 list_cmd_options(stdout, long_options);
162 goto end;
163 default:
164 usage(stderr);
165 ret = CMD_UNDEFINED;
166 goto end;
167 }
168 }
169
170 opt_channels = (char*) poptGetArg(pc);
171 if (opt_channels == NULL) {
172 ERR("Missing channel name(s).\n");
173 usage(stderr);
174 ret = CMD_ERROR;
175 goto end;
176 }
177
178 if (!opt_session_name) {
179 session_name = get_session_name();
180 if (session_name == NULL) {
181 ret = CMD_ERROR;
182 goto end;
183 }
184 } else {
185 session_name = opt_session_name;
186 }
187
188 ret = disable_channels(session_name);
189
190 end:
191 if (!opt_session_name && session_name) {
192 free(session_name);
193 }
194 poptFreeContext(pc);
195 return ret;
196 }
This page took 0.038466 seconds and 4 git commands to generate.