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