Commit | Line | Data |
---|---|---|
26cc6b4e DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
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. | |
26cc6b4e DG |
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 | * | |
d14d33bf AM |
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. | |
26cc6b4e DG |
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 | ||
c399183f | 27 | #include "../command.h" |
26cc6b4e DG |
28 | |
29 | static char *opt_channels; | |
e14f64a8 | 30 | static int opt_kernel; |
5440dc42 | 31 | static char *opt_session_name; |
26cc6b4e | 32 | static int opt_userspace; |
d78d6610 DG |
33 | #if 0 |
34 | /* Not implemented yet */ | |
eeac7d46 | 35 | static char *opt_cmd_name; |
26cc6b4e | 36 | static pid_t opt_pid; |
d78d6610 | 37 | #endif |
26cc6b4e DG |
38 | |
39 | enum { | |
40 | OPT_HELP = 1, | |
41 | OPT_USERSPACE, | |
679b4943 | 42 | OPT_LIST_OPTIONS, |
26cc6b4e DG |
43 | }; |
44 | ||
cd80958d DG |
45 | static struct lttng_handle *handle; |
46 | ||
26cc6b4e DG |
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}, | |
5440dc42 | 50 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
26cc6b4e | 51 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
d78d6610 DG |
52 | #if 0 |
53 | /* Not implemented yet */ | |
6caa2bcc | 54 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
26cc6b4e | 55 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
d78d6610 DG |
56 | #else |
57 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, | |
58 | #endif | |
679b4943 | 59 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
26cc6b4e DG |
60 | {0, 0, 0, 0, 0, 0, 0} |
61 | }; | |
62 | ||
63 | /* | |
64 | * usage | |
65 | */ | |
66 | static void usage(FILE *ofp) | |
67 | { | |
32a6298d | 68 | fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] [-k|-u] [OPTIONS]\n"); |
26cc6b4e | 69 | fprintf(ofp, "\n"); |
32a6298d | 70 | fprintf(ofp, "Options:\n"); |
78f0bacd | 71 | fprintf(ofp, " -h, --help Show this help\n"); |
679b4943 | 72 | fprintf(ofp, " --list-options Simple listing of options\n"); |
32a6298d | 73 | fprintf(ofp, " -s, --session NAME Apply to session name\n"); |
27221701 | 74 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
27221701 | 75 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
26cc6b4e DG |
76 | fprintf(ofp, "\n"); |
77 | } | |
78 | ||
79 | /* | |
cd80958d | 80 | * Disabling channel using the lttng API. |
26cc6b4e | 81 | */ |
cd80958d | 82 | static int disable_channels(char *session_name) |
26cc6b4e | 83 | { |
ae856491 | 84 | int ret = CMD_SUCCESS, warn = 0; |
26cc6b4e | 85 | char *channel_name; |
7d29a247 | 86 | struct lttng_domain dom; |
26cc6b4e | 87 | |
441c16a7 MD |
88 | memset(&dom, 0, sizeof(dom)); |
89 | ||
d78d6610 | 90 | /* Create lttng domain */ |
7d29a247 DG |
91 | if (opt_kernel) { |
92 | dom.type = LTTNG_DOMAIN_KERNEL; | |
d78d6610 | 93 | } else if (opt_userspace) { |
78f0bacd | 94 | dom.type = LTTNG_DOMAIN_UST; |
78f0bacd | 95 | } else { |
e14f64a8 | 96 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
d16c1a4c | 97 | ret = CMD_ERROR; |
78f0bacd | 98 | goto error; |
7d29a247 DG |
99 | } |
100 | ||
cd80958d DG |
101 | handle = lttng_create_handle(session_name, &dom); |
102 | if (handle == NULL) { | |
103 | ret = -1; | |
104 | goto error; | |
105 | } | |
106 | ||
26cc6b4e DG |
107 | /* Strip channel list */ |
108 | channel_name = strtok(opt_channels, ","); | |
109 | while (channel_name != NULL) { | |
78f0bacd DG |
110 | DBG("Disabling channel %s", channel_name); |
111 | ||
112 | ret = lttng_disable_channel(handle, channel_name); | |
113 | if (ret < 0) { | |
ae856491 DG |
114 | ERR("Channel %s: %s (session %s)", channel_name, |
115 | lttng_strerror(ret), session_name); | |
116 | warn = 1; | |
26cc6b4e | 117 | } else { |
7885e399 | 118 | MSG("%s channel %s disabled for session %s", |
ae856491 | 119 | opt_kernel ? "Kernel" : "UST", channel_name, session_name); |
26cc6b4e DG |
120 | } |
121 | ||
122 | /* Next channel */ | |
123 | channel_name = strtok(NULL, ","); | |
124 | } | |
125 | ||
ae856491 DG |
126 | ret = CMD_SUCCESS; |
127 | ||
26cc6b4e | 128 | error: |
ae856491 DG |
129 | if (warn) { |
130 | ret = CMD_WARNING; | |
131 | } | |
132 | ||
cd80958d DG |
133 | lttng_destroy_handle(handle); |
134 | ||
26cc6b4e DG |
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 | { | |
ca1c3607 | 145 | int opt, ret = CMD_SUCCESS; |
26cc6b4e | 146 | static poptContext pc; |
cd80958d | 147 | char *session_name = NULL; |
26cc6b4e DG |
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: | |
ca1c3607 | 155 | usage(stdout); |
26cc6b4e DG |
156 | goto end; |
157 | case OPT_USERSPACE: | |
158 | opt_userspace = 1; | |
159 | break; | |
679b4943 SM |
160 | case OPT_LIST_OPTIONS: |
161 | list_cmd_options(stdout, long_options); | |
679b4943 | 162 | goto end; |
26cc6b4e DG |
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); | |
ca1c3607 | 174 | ret = CMD_ERROR; |
26cc6b4e DG |
175 | goto end; |
176 | } | |
177 | ||
cd80958d DG |
178 | if (!opt_session_name) { |
179 | session_name = get_session_name(); | |
180 | if (session_name == NULL) { | |
ca1c3607 | 181 | ret = CMD_ERROR; |
cd80958d DG |
182 | goto end; |
183 | } | |
184 | } else { | |
185 | session_name = opt_session_name; | |
186 | } | |
187 | ||
188 | ret = disable_channels(session_name); | |
26cc6b4e DG |
189 | |
190 | end: | |
5853fd43 DG |
191 | if (!opt_session_name && session_name) { |
192 | free(session_name); | |
193 | } | |
ca1c3607 | 194 | poptFreeContext(pc); |
26cc6b4e DG |
195 | return ret; |
196 | } |