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