Commit | Line | Data |
---|---|---|
c39c72ee PMF |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
872037bb PMF |
18 | #define _GNU_SOURCE |
19 | #include <stdio.h> | |
fbd8191b PMF |
20 | #include <unistd.h> |
21 | #include <getopt.h> | |
22 | #include <stdlib.h> | |
fbd8191b | 23 | #include <fcntl.h> |
fbd8191b | 24 | |
f9e5ce61 | 25 | #include "ustcomm.h" |
ae937656 | 26 | #include "ustcmd.h" |
2028e7fd | 27 | #include "usterr.h" |
ae937656 PP |
28 | |
29 | enum command { | |
b0a5a08b PMF |
30 | CREATE_TRACE=1000, |
31 | ALLOC_TRACE, | |
ae937656 PP |
32 | START_TRACE, |
33 | STOP_TRACE, | |
b0a5a08b | 34 | DESTROY_TRACE, |
ae937656 PP |
35 | LIST_MARKERS, |
36 | ENABLE_MARKER, | |
37 | DISABLE_MARKER, | |
38 | GET_ONLINE_PIDS, | |
763f41e5 DS |
39 | SET_SUBBUF_SIZE, |
40 | SET_SUBBUF_NUM, | |
e77b8e8e DS |
41 | GET_SUBBUF_SIZE, |
42 | GET_SUBBUF_NUM, | |
ae937656 | 43 | UNKNOWN |
ef290fca | 44 | }; |
fbd8191b | 45 | |
52c51a47 | 46 | struct ust_opts { |
ae937656 | 47 | enum command cmd; |
52c51a47 | 48 | pid_t *pids; |
08230db7 | 49 | char *regex; |
52c51a47 PMF |
50 | }; |
51 | ||
fd2fb4f9 PMF |
52 | char *progname = NULL; |
53 | ||
54 | void usage(void) | |
55 | { | |
ae937656 | 56 | fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); |
fd2fb4f9 PMF |
57 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
58 | \n\ | |
59 | Commands:\n\ | |
62ec620f | 60 | --create-trace\t\t\tCreate trace\n\ |
4ed9f99d | 61 | --alloc-trace\t\t\tAlloc trace\n\ |
fd2fb4f9 PMF |
62 | --start-trace\t\t\tStart tracing\n\ |
63 | --stop-trace\t\t\tStop tracing\n\ | |
64 | --destroy-trace\t\t\tDestroy the trace\n\ | |
763f41e5 DS |
65 | --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\ |
66 | --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\ | |
e77b8e8e DS |
67 | --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\ |
68 | --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\ | |
ae937656 PP |
69 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
70 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ | |
71 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ | |
72 | \ | |
fd2fb4f9 PMF |
73 | "); |
74 | } | |
75 | ||
52c51a47 | 76 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
fbd8191b | 77 | { |
52c51a47 | 78 | int c; |
52c51a47 | 79 | |
52c51a47 | 80 | opts->pids = NULL; |
ef290fca | 81 | opts->regex = NULL; |
52c51a47 PMF |
82 | |
83 | while (1) { | |
52c51a47 PMF |
84 | int option_index = 0; |
85 | static struct option long_options[] = { | |
b0a5a08b PMF |
86 | { "create-trace", 0, 0, CREATE_TRACE }, |
87 | { "alloc-trace", 0, 0, ALLOC_TRACE }, | |
88 | { "start-trace", 0, 0, START_TRACE }, | |
89 | { "stop-trace", 0, 0, STOP_TRACE }, | |
90 | { "destroy-trace", 0, 0, DESTROY_TRACE }, | |
91 | { "list-markers", 0, 0, LIST_MARKERS }, | |
92 | { "enable-marker", 1, 0, ENABLE_MARKER }, | |
93 | { "disable-marker", 1, 0, DISABLE_MARKER }, | |
94 | { "help", 0, 0, 'h' }, | |
95 | { "online-pids", 0, 0, GET_ONLINE_PIDS }, | |
96 | { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE }, | |
97 | { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM }, | |
e77b8e8e DS |
98 | { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE }, |
99 | { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM }, | |
b0a5a08b | 100 | { 0, 0, 0, 0 } |
52c51a47 PMF |
101 | }; |
102 | ||
d373b5cd | 103 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
52c51a47 PMF |
104 | if (c == -1) |
105 | break; | |
106 | ||
b0a5a08b PMF |
107 | if(c >= 1000) |
108 | opts->cmd = c; | |
109 | ||
52c51a47 PMF |
110 | switch (c) { |
111 | case 0: | |
112 | printf("option %s", long_options[option_index].name); | |
113 | if (optarg) | |
114 | printf(" with arg %s", optarg); | |
115 | printf("\n"); | |
116 | break; | |
117 | ||
b0a5a08b PMF |
118 | case ENABLE_MARKER: |
119 | case DISABLE_MARKER: | |
120 | case SET_SUBBUF_SIZE: | |
121 | case SET_SUBBUF_NUM: | |
e77b8e8e DS |
122 | case GET_SUBBUF_SIZE: |
123 | case GET_SUBBUF_NUM: | |
ef290fca | 124 | opts->regex = strdup(optarg); |
ae937656 | 125 | break; |
b0a5a08b | 126 | |
d373b5cd PMF |
127 | case 'h': |
128 | usage(); | |
129 | exit(0); | |
b0a5a08b PMF |
130 | |
131 | case '?': | |
132 | fprintf(stderr, "Invalid argument\n\n"); | |
133 | usage(); | |
134 | exit(1); | |
fbd8191b PMF |
135 | } |
136 | } | |
137 | ||
772030fe | 138 | if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
139 | int i; |
140 | int pididx=0; | |
141 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); | |
fbd8191b | 142 | |
52c51a47 | 143 | for(i=optind; i<argc; i++) { |
08230db7 PMF |
144 | /* don't take any chances, use a long long */ |
145 | long long tmp; | |
146 | char *endptr; | |
147 | tmp = strtoull(argv[i], &endptr, 10); | |
148 | if(*endptr != '\0') { | |
149 | ERR("The pid \"%s\" is invalid.", argv[i]); | |
150 | return 1; | |
151 | } | |
152 | opts->pids[pididx++] = (pid_t) tmp; | |
52c51a47 PMF |
153 | } |
154 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
155 | } |
156 | ||
52c51a47 PMF |
157 | return 0; |
158 | } | |
fbd8191b | 159 | |
fbd8191b PMF |
160 | int main(int argc, char *argv[]) |
161 | { | |
52c51a47 | 162 | pid_t *pidit; |
52c51a47 PMF |
163 | int result; |
164 | struct ust_opts opts; | |
fbd8191b | 165 | |
52c51a47 | 166 | progname = argv[0]; |
fbd8191b | 167 | |
52c51a47 PMF |
168 | if(argc <= 1) { |
169 | fprintf(stderr, "No operation specified.\n"); | |
170 | usage(); | |
171 | exit(EXIT_FAILURE); | |
172 | } | |
173 | ||
174 | result = parse_opts_long(argc, argv, &opts); | |
175 | if(result) { | |
08230db7 | 176 | fprintf(stderr, "\n"); |
52c51a47 PMF |
177 | usage(); |
178 | exit(EXIT_FAILURE); | |
179 | } | |
180 | ||
ae937656 | 181 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
182 | fprintf(stderr, "No pid specified.\n"); |
183 | usage(); | |
184 | exit(EXIT_FAILURE); | |
185 | } | |
ae937656 | 186 | if(opts.cmd == UNKNOWN) { |
52c51a47 PMF |
187 | fprintf(stderr, "No command specified.\n"); |
188 | usage(); | |
189 | exit(EXIT_FAILURE); | |
190 | } | |
ae937656 | 191 | if (opts.cmd == GET_ONLINE_PIDS) { |
08230db7 | 192 | pid_t *pp = ustcmd_get_online_pids(); |
ae937656 | 193 | unsigned int i = 0; |
52c51a47 | 194 | |
ae937656 PP |
195 | if (pp) { |
196 | while (pp[i] != 0) { | |
197 | printf("%u\n", (unsigned int) pp[i]); | |
198 | ++i; | |
199 | } | |
200 | free(pp); | |
52c51a47 | 201 | } |
ef290fca | 202 | |
ae937656 PP |
203 | exit(EXIT_SUCCESS); |
204 | } | |
52c51a47 | 205 | |
ae937656 | 206 | pidit = opts.pids; |
08230db7 | 207 | struct marker_status *cmsf = NULL; |
ef290fca | 208 | |
ae937656 PP |
209 | while(*pidit != -1) { |
210 | switch (opts.cmd) { | |
62ec620f PMF |
211 | case CREATE_TRACE: |
212 | result = ustcmd_create_trace(*pidit); | |
213 | if (result) { | |
214 | ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit); | |
215 | break; | |
216 | } | |
217 | break; | |
218 | ||
ae937656 | 219 | case START_TRACE: |
08230db7 PMF |
220 | result = ustcmd_start_trace(*pidit); |
221 | if (result) { | |
222 | ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit); | |
223 | break; | |
224 | } | |
ae937656 | 225 | break; |
ef290fca | 226 | |
ae937656 | 227 | case STOP_TRACE: |
08230db7 PMF |
228 | result = ustcmd_stop_trace(*pidit); |
229 | if (result) { | |
230 | ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit); | |
231 | break; | |
232 | } | |
ae937656 | 233 | break; |
ef290fca | 234 | |
b0a5a08b | 235 | case DESTROY_TRACE: |
08230db7 PMF |
236 | result = ustcmd_destroy_trace(*pidit); |
237 | if (result) { | |
238 | ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit); | |
239 | break; | |
240 | } | |
ae937656 | 241 | break; |
ef290fca | 242 | |
ae937656 | 243 | case LIST_MARKERS: |
08230db7 PMF |
244 | cmsf = NULL; |
245 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { | |
246 | fprintf(stderr, | |
247 | "error while trying to list markers for" | |
248 | " PID %u\n", (unsigned int) *pidit); | |
249 | break; | |
250 | } | |
251 | unsigned int i = 0; | |
252 | while (cmsf[i].channel != NULL) { | |
253 | printf("{PID: %u, channel/marker: %s/%s, " | |
264f6231 | 254 | "state: %u, fmt: %s}\n", |
08230db7 PMF |
255 | (unsigned int) *pidit, |
256 | cmsf[i].channel, | |
257 | cmsf[i].marker, | |
258 | cmsf[i].state, | |
259 | cmsf[i].fs); | |
260 | ++i; | |
261 | } | |
262 | ustcmd_free_cmsf(cmsf); | |
ae937656 | 263 | break; |
ef290fca | 264 | |
ae937656 | 265 | case ENABLE_MARKER: |
ee648b8f PMF |
266 | if(opts.regex) |
267 | ustcmd_set_marker_state(opts.regex, 1, *pidit); | |
268 | break; | |
ae937656 | 269 | case DISABLE_MARKER: |
ee648b8f PMF |
270 | if(opts.regex) |
271 | ustcmd_set_marker_state(opts.regex, 0, *pidit); | |
272 | break; | |
ef290fca | 273 | |
763f41e5 DS |
274 | case SET_SUBBUF_SIZE: |
275 | ustcmd_set_subbuf_size(opts.regex, *pidit); | |
276 | break; | |
277 | ||
278 | case SET_SUBBUF_NUM: | |
279 | ustcmd_set_subbuf_num(opts.regex, *pidit); | |
280 | break; | |
281 | ||
e77b8e8e DS |
282 | case GET_SUBBUF_SIZE: |
283 | result = ustcmd_get_subbuf_size(opts.regex, *pidit); | |
284 | if (result == -1) { | |
285 | ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit); | |
286 | break; | |
287 | } | |
288 | ||
289 | printf("the size of subbufers is %d\n", result); | |
290 | break; | |
291 | ||
292 | case GET_SUBBUF_NUM: | |
293 | result = ustcmd_get_subbuf_num(opts.regex, *pidit); | |
294 | if (result == -1) { | |
295 | ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit); | |
296 | break; | |
297 | } | |
298 | ||
299 | printf("the number of subbufers is %d\n", result); | |
300 | break; | |
301 | ||
763f41e5 DS |
302 | case ALLOC_TRACE: |
303 | result = ustcmd_alloc_trace(*pidit); | |
304 | if (result) { | |
305 | ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit); | |
306 | break; | |
307 | } | |
308 | break; | |
309 | ||
ae937656 | 310 | default: |
08230db7 | 311 | ERR("unknown command\n"); |
ae937656 PP |
312 | break; |
313 | } | |
ef290fca | 314 | |
52c51a47 PMF |
315 | pidit++; |
316 | } | |
82b1a169 | 317 | |
ef290fca PMF |
318 | if (opts.pids != NULL) { |
319 | free(opts.pids); | |
320 | } | |
321 | if (opts.regex != NULL) { | |
322 | free(opts.regex); | |
ae937656 | 323 | } |
fbd8191b PMF |
324 | |
325 | return 0; | |
326 | } | |
ae937656 | 327 |