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 | |
d6c9f207 | 25 | #include "ust/ustcmd.h" |
2028e7fd | 26 | #include "usterr.h" |
ae937656 PP |
27 | |
28 | enum command { | |
b0a5a08b PMF |
29 | CREATE_TRACE=1000, |
30 | ALLOC_TRACE, | |
ae937656 PP |
31 | START_TRACE, |
32 | STOP_TRACE, | |
b0a5a08b | 33 | DESTROY_TRACE, |
ae937656 PP |
34 | LIST_MARKERS, |
35 | ENABLE_MARKER, | |
36 | DISABLE_MARKER, | |
37 | GET_ONLINE_PIDS, | |
763f41e5 DS |
38 | SET_SUBBUF_SIZE, |
39 | SET_SUBBUF_NUM, | |
e77b8e8e DS |
40 | GET_SUBBUF_SIZE, |
41 | GET_SUBBUF_NUM, | |
b2fb2f91 AH |
42 | GET_SOCK_PATH, |
43 | SET_SOCK_PATH, | |
b9318b35 | 44 | FORCE_SWITCH, |
ae937656 | 45 | UNKNOWN |
ef290fca | 46 | }; |
fbd8191b | 47 | |
52c51a47 | 48 | struct ust_opts { |
ae937656 | 49 | enum command cmd; |
52c51a47 | 50 | pid_t *pids; |
08230db7 | 51 | char *regex; |
52c51a47 PMF |
52 | }; |
53 | ||
fd2fb4f9 PMF |
54 | char *progname = NULL; |
55 | ||
56 | void usage(void) | |
57 | { | |
ae937656 | 58 | fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); |
fd2fb4f9 PMF |
59 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
60 | \n\ | |
61 | Commands:\n\ | |
62ec620f | 62 | --create-trace\t\t\tCreate trace\n\ |
4ed9f99d | 63 | --alloc-trace\t\t\tAlloc trace\n\ |
fd2fb4f9 PMF |
64 | --start-trace\t\t\tStart tracing\n\ |
65 | --stop-trace\t\t\tStop tracing\n\ | |
66 | --destroy-trace\t\t\tDestroy the trace\n\ | |
763f41e5 DS |
67 | --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\ |
68 | --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\ | |
b2fb2f91 | 69 | --set-sock-path\t\t\tSet the path of the daemon socket\n\ |
e77b8e8e DS |
70 | --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\ |
71 | --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\ | |
b2fb2f91 | 72 | --get-sock-path\t\t\tGet the path of the daemon socket\n\ |
ae937656 PP |
73 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
74 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ | |
75 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ | |
b9318b35 | 76 | --force-switch\t\t\tForce a subbuffer switch\n\ |
ae937656 | 77 | \ |
fd2fb4f9 PMF |
78 | "); |
79 | } | |
80 | ||
52c51a47 | 81 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
fbd8191b | 82 | { |
52c51a47 | 83 | int c; |
52c51a47 | 84 | |
52c51a47 | 85 | opts->pids = NULL; |
ef290fca | 86 | opts->regex = NULL; |
52c51a47 PMF |
87 | |
88 | while (1) { | |
52c51a47 PMF |
89 | int option_index = 0; |
90 | static struct option long_options[] = { | |
b0a5a08b PMF |
91 | { "create-trace", 0, 0, CREATE_TRACE }, |
92 | { "alloc-trace", 0, 0, ALLOC_TRACE }, | |
93 | { "start-trace", 0, 0, START_TRACE }, | |
94 | { "stop-trace", 0, 0, STOP_TRACE }, | |
95 | { "destroy-trace", 0, 0, DESTROY_TRACE }, | |
96 | { "list-markers", 0, 0, LIST_MARKERS }, | |
97 | { "enable-marker", 1, 0, ENABLE_MARKER }, | |
98 | { "disable-marker", 1, 0, DISABLE_MARKER }, | |
99 | { "help", 0, 0, 'h' }, | |
100 | { "online-pids", 0, 0, GET_ONLINE_PIDS }, | |
101 | { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE }, | |
102 | { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM }, | |
e77b8e8e DS |
103 | { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE }, |
104 | { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM }, | |
b2fb2f91 AH |
105 | { "get-sock-path", 0, 0, GET_SOCK_PATH }, |
106 | { "set-sock-path", 1, 0, SET_SOCK_PATH }, | |
b9318b35 | 107 | { "force-switch", 0, 0, FORCE_SWITCH }, |
b0a5a08b | 108 | { 0, 0, 0, 0 } |
52c51a47 PMF |
109 | }; |
110 | ||
d373b5cd | 111 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
52c51a47 PMF |
112 | if (c == -1) |
113 | break; | |
114 | ||
b0a5a08b PMF |
115 | if(c >= 1000) |
116 | opts->cmd = c; | |
117 | ||
52c51a47 PMF |
118 | switch (c) { |
119 | case 0: | |
120 | printf("option %s", long_options[option_index].name); | |
121 | if (optarg) | |
122 | printf(" with arg %s", optarg); | |
123 | printf("\n"); | |
124 | break; | |
125 | ||
b0a5a08b PMF |
126 | case ENABLE_MARKER: |
127 | case DISABLE_MARKER: | |
128 | case SET_SUBBUF_SIZE: | |
129 | case SET_SUBBUF_NUM: | |
e77b8e8e DS |
130 | case GET_SUBBUF_SIZE: |
131 | case GET_SUBBUF_NUM: | |
b2fb2f91 | 132 | case SET_SOCK_PATH: |
ef290fca | 133 | opts->regex = strdup(optarg); |
ae937656 | 134 | break; |
b0a5a08b | 135 | |
d373b5cd PMF |
136 | case 'h': |
137 | usage(); | |
138 | exit(0); | |
b0a5a08b PMF |
139 | |
140 | case '?': | |
141 | fprintf(stderr, "Invalid argument\n\n"); | |
142 | usage(); | |
143 | exit(1); | |
fbd8191b PMF |
144 | } |
145 | } | |
146 | ||
772030fe | 147 | if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
148 | int i; |
149 | int pididx=0; | |
150 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); | |
fbd8191b | 151 | |
52c51a47 | 152 | for(i=optind; i<argc; i++) { |
08230db7 PMF |
153 | /* don't take any chances, use a long long */ |
154 | long long tmp; | |
155 | char *endptr; | |
156 | tmp = strtoull(argv[i], &endptr, 10); | |
157 | if(*endptr != '\0') { | |
158 | ERR("The pid \"%s\" is invalid.", argv[i]); | |
159 | return 1; | |
160 | } | |
161 | opts->pids[pididx++] = (pid_t) tmp; | |
52c51a47 PMF |
162 | } |
163 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
164 | } |
165 | ||
52c51a47 PMF |
166 | return 0; |
167 | } | |
fbd8191b | 168 | |
fbd8191b PMF |
169 | int main(int argc, char *argv[]) |
170 | { | |
52c51a47 | 171 | pid_t *pidit; |
52c51a47 | 172 | int result; |
b2fb2f91 | 173 | char *tmp; |
52c51a47 | 174 | struct ust_opts opts; |
fbd8191b | 175 | |
52c51a47 | 176 | progname = argv[0]; |
fbd8191b | 177 | |
52c51a47 PMF |
178 | if(argc <= 1) { |
179 | fprintf(stderr, "No operation specified.\n"); | |
180 | usage(); | |
181 | exit(EXIT_FAILURE); | |
182 | } | |
183 | ||
184 | result = parse_opts_long(argc, argv, &opts); | |
185 | if(result) { | |
08230db7 | 186 | fprintf(stderr, "\n"); |
52c51a47 PMF |
187 | usage(); |
188 | exit(EXIT_FAILURE); | |
189 | } | |
190 | ||
ae937656 | 191 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
192 | fprintf(stderr, "No pid specified.\n"); |
193 | usage(); | |
194 | exit(EXIT_FAILURE); | |
195 | } | |
ae937656 | 196 | if(opts.cmd == UNKNOWN) { |
52c51a47 PMF |
197 | fprintf(stderr, "No command specified.\n"); |
198 | usage(); | |
199 | exit(EXIT_FAILURE); | |
200 | } | |
ae937656 | 201 | if (opts.cmd == GET_ONLINE_PIDS) { |
08230db7 | 202 | pid_t *pp = ustcmd_get_online_pids(); |
ae937656 | 203 | unsigned int i = 0; |
52c51a47 | 204 | |
ae937656 PP |
205 | if (pp) { |
206 | while (pp[i] != 0) { | |
207 | printf("%u\n", (unsigned int) pp[i]); | |
208 | ++i; | |
209 | } | |
210 | free(pp); | |
52c51a47 | 211 | } |
ef290fca | 212 | |
ae937656 PP |
213 | exit(EXIT_SUCCESS); |
214 | } | |
52c51a47 | 215 | |
ae937656 | 216 | pidit = opts.pids; |
08230db7 | 217 | struct marker_status *cmsf = NULL; |
ef290fca | 218 | |
ae937656 PP |
219 | while(*pidit != -1) { |
220 | switch (opts.cmd) { | |
62ec620f PMF |
221 | case CREATE_TRACE: |
222 | result = ustcmd_create_trace(*pidit); | |
223 | if (result) { | |
224 | ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit); | |
225 | break; | |
226 | } | |
227 | break; | |
228 | ||
ae937656 | 229 | case START_TRACE: |
08230db7 PMF |
230 | result = ustcmd_start_trace(*pidit); |
231 | if (result) { | |
232 | ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit); | |
233 | break; | |
234 | } | |
ae937656 | 235 | break; |
ef290fca | 236 | |
ae937656 | 237 | case STOP_TRACE: |
08230db7 PMF |
238 | result = ustcmd_stop_trace(*pidit); |
239 | if (result) { | |
240 | ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit); | |
241 | break; | |
242 | } | |
ae937656 | 243 | break; |
ef290fca | 244 | |
b0a5a08b | 245 | case DESTROY_TRACE: |
08230db7 PMF |
246 | result = ustcmd_destroy_trace(*pidit); |
247 | if (result) { | |
248 | ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit); | |
249 | break; | |
250 | } | |
ae937656 | 251 | break; |
ef290fca | 252 | |
ae937656 | 253 | case LIST_MARKERS: |
08230db7 PMF |
254 | cmsf = NULL; |
255 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { | |
256 | fprintf(stderr, | |
257 | "error while trying to list markers for" | |
258 | " PID %u\n", (unsigned int) *pidit); | |
259 | break; | |
260 | } | |
261 | unsigned int i = 0; | |
262 | while (cmsf[i].channel != NULL) { | |
263 | printf("{PID: %u, channel/marker: %s/%s, " | |
264f6231 | 264 | "state: %u, fmt: %s}\n", |
08230db7 PMF |
265 | (unsigned int) *pidit, |
266 | cmsf[i].channel, | |
267 | cmsf[i].marker, | |
268 | cmsf[i].state, | |
269 | cmsf[i].fs); | |
270 | ++i; | |
271 | } | |
272 | ustcmd_free_cmsf(cmsf); | |
ae937656 | 273 | break; |
ef290fca | 274 | |
ae937656 | 275 | case ENABLE_MARKER: |
ee648b8f PMF |
276 | if(opts.regex) |
277 | ustcmd_set_marker_state(opts.regex, 1, *pidit); | |
278 | break; | |
ae937656 | 279 | case DISABLE_MARKER: |
ee648b8f PMF |
280 | if(opts.regex) |
281 | ustcmd_set_marker_state(opts.regex, 0, *pidit); | |
282 | break; | |
ef290fca | 283 | |
763f41e5 DS |
284 | case SET_SUBBUF_SIZE: |
285 | ustcmd_set_subbuf_size(opts.regex, *pidit); | |
286 | break; | |
287 | ||
288 | case SET_SUBBUF_NUM: | |
289 | ustcmd_set_subbuf_num(opts.regex, *pidit); | |
290 | break; | |
291 | ||
e77b8e8e DS |
292 | case GET_SUBBUF_SIZE: |
293 | result = ustcmd_get_subbuf_size(opts.regex, *pidit); | |
294 | if (result == -1) { | |
295 | ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit); | |
296 | break; | |
297 | } | |
298 | ||
299 | printf("the size of subbufers is %d\n", result); | |
300 | break; | |
301 | ||
302 | case GET_SUBBUF_NUM: | |
303 | result = ustcmd_get_subbuf_num(opts.regex, *pidit); | |
304 | if (result == -1) { | |
305 | ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit); | |
306 | break; | |
307 | } | |
308 | ||
309 | printf("the number of subbufers is %d\n", result); | |
310 | break; | |
311 | ||
763f41e5 DS |
312 | case ALLOC_TRACE: |
313 | result = ustcmd_alloc_trace(*pidit); | |
314 | if (result) { | |
315 | ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit); | |
316 | break; | |
317 | } | |
318 | break; | |
319 | ||
b2fb2f91 AH |
320 | case GET_SOCK_PATH: |
321 | result = ustcmd_get_sock_path(&tmp, *pidit); | |
322 | if (result) { | |
323 | ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit); | |
324 | break; | |
325 | } | |
326 | printf("the socket path is %s\n", tmp); | |
327 | free(tmp); | |
328 | break; | |
329 | ||
330 | case SET_SOCK_PATH: | |
331 | result = ustcmd_set_sock_path(opts.regex, *pidit); | |
332 | if (result) { | |
333 | ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit); | |
334 | } | |
335 | break; | |
336 | ||
b9318b35 AH |
337 | case FORCE_SWITCH: |
338 | result = ustcmd_force_switch(*pidit); | |
339 | if (result) { | |
340 | ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit); | |
341 | } | |
342 | break; | |
343 | ||
ae937656 | 344 | default: |
08230db7 | 345 | ERR("unknown command\n"); |
ae937656 PP |
346 | break; |
347 | } | |
ef290fca | 348 | |
52c51a47 PMF |
349 | pidit++; |
350 | } | |
82b1a169 | 351 | |
ef290fca PMF |
352 | if (opts.pids != NULL) { |
353 | free(opts.pids); | |
354 | } | |
355 | if (opts.regex != NULL) { | |
356 | free(opts.regex); | |
ae937656 | 357 | } |
fbd8191b PMF |
358 | |
359 | return 0; | |
360 | } | |
ae937656 | 361 |