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> |
ef290fca | 24 | #include <regex.h> |
fbd8191b | 25 | |
f9e5ce61 | 26 | #include "ustcomm.h" |
ae937656 | 27 | #include "ustcmd.h" |
2028e7fd | 28 | #include "usterr.h" |
ae937656 PP |
29 | |
30 | enum command { | |
31 | START_TRACE, | |
32 | STOP_TRACE, | |
33 | START, | |
34 | DESTROY, | |
35 | LIST_MARKERS, | |
36 | ENABLE_MARKER, | |
37 | DISABLE_MARKER, | |
38 | GET_ONLINE_PIDS, | |
39 | UNKNOWN | |
ef290fca | 40 | }; |
fbd8191b | 41 | |
52c51a47 | 42 | struct ust_opts { |
ae937656 | 43 | enum command cmd; |
52c51a47 | 44 | pid_t *pids; |
ef290fca PMF |
45 | char* regex; |
46 | regex_t preg; | |
47 | int regex_state; | |
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\ | |
58 | --start-trace\t\t\tStart tracing\n\ | |
59 | --stop-trace\t\t\tStop tracing\n\ | |
60 | --destroy-trace\t\t\tDestroy the trace\n\ | |
ae937656 PP |
61 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
62 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ | |
63 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ | |
64 | \ | |
fd2fb4f9 PMF |
65 | "); |
66 | } | |
67 | ||
52c51a47 | 68 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
fbd8191b | 69 | { |
52c51a47 | 70 | int c; |
52c51a47 | 71 | |
52c51a47 | 72 | opts->pids = NULL; |
ef290fca PMF |
73 | opts->regex = NULL; |
74 | opts->regex_state = -1; | |
52c51a47 PMF |
75 | |
76 | while (1) { | |
52c51a47 PMF |
77 | int option_index = 0; |
78 | static struct option long_options[] = { | |
79 | {"start-trace", 0, 0, 1000}, | |
80 | {"stop-trace", 0, 0, 1001}, | |
81 | {"destroy-trace", 0, 0, 1002}, | |
82 | {"list-markers", 0, 0, 1004}, | |
83 | {"print-markers", 0, 0, 1005}, | |
84 | {"pid", 1, 0, 1006}, | |
85 | {"enable-marker", 1, 0, 1007}, | |
86 | {"disable-marker", 1, 0, 1008}, | |
87 | {"start", 0, 0, 1009}, | |
d373b5cd PMF |
88 | {"help", 0, 0, 'h'}, |
89 | {"version", 0, 0, 1010}, | |
ae937656 | 90 | {"online-pids", 0, 0, 1011}, |
52c51a47 PMF |
91 | {0, 0, 0, 0} |
92 | }; | |
93 | ||
d373b5cd | 94 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
52c51a47 PMF |
95 | if (c == -1) |
96 | break; | |
97 | ||
98 | switch (c) { | |
99 | case 0: | |
100 | printf("option %s", long_options[option_index].name); | |
101 | if (optarg) | |
102 | printf(" with arg %s", optarg); | |
103 | printf("\n"); | |
104 | break; | |
105 | ||
106 | case 1000: | |
ae937656 | 107 | opts->cmd = START_TRACE; |
52c51a47 PMF |
108 | break; |
109 | case 1001: | |
ae937656 | 110 | opts->cmd = STOP_TRACE; |
52c51a47 PMF |
111 | break; |
112 | case 1009: | |
ae937656 | 113 | opts->cmd = START; |
52c51a47 PMF |
114 | break; |
115 | case 1002: | |
ae937656 | 116 | opts->cmd = DESTROY; |
52c51a47 PMF |
117 | break; |
118 | case 1004: | |
ae937656 | 119 | opts->cmd = LIST_MARKERS; |
52c51a47 PMF |
120 | break; |
121 | case 1007: | |
ae937656 | 122 | opts->cmd = ENABLE_MARKER; |
ef290fca | 123 | opts->regex = strdup(optarg); |
52c51a47 PMF |
124 | break; |
125 | case 1008: | |
ae937656 | 126 | opts->cmd = DISABLE_MARKER; |
ef290fca | 127 | opts->regex = strdup(optarg); |
ae937656 PP |
128 | break; |
129 | case 1011: | |
130 | opts->cmd = GET_ONLINE_PIDS; | |
52c51a47 | 131 | break; |
d373b5cd PMF |
132 | case 'h': |
133 | usage(); | |
134 | exit(0); | |
135 | case 1010: | |
ae937656 | 136 | printf("Version 0.1\n"); |
52c51a47 PMF |
137 | |
138 | default: | |
ae937656 PP |
139 | /* unknown option or other error; error is |
140 | printed by getopt, just return */ | |
141 | opts->cmd = UNKNOWN; | |
52c51a47 | 142 | return 1; |
fbd8191b PMF |
143 | } |
144 | } | |
145 | ||
772030fe | 146 | if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
147 | int i; |
148 | int pididx=0; | |
149 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); | |
fbd8191b | 150 | |
52c51a47 PMF |
151 | for(i=optind; i<argc; i++) { |
152 | opts->pids[pididx++] = atoi(argv[i]); | |
153 | } | |
154 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
155 | } |
156 | ||
772030fe PMF |
157 | if (opts->cmd == ENABLE_MARKER || opts->cmd == DISABLE_MARKER) { |
158 | if (opts->regex_state = regcomp(&opts->preg, opts->regex, 0)) { | |
159 | fprintf(stderr, "Invalid regular expression.\n"); | |
160 | } | |
161 | } | |
162 | ||
52c51a47 PMF |
163 | return 0; |
164 | } | |
fbd8191b | 165 | |
772030fe PMF |
166 | static void regex_change_m_state(struct ust_opts* opts, pid_t pid) { |
167 | struct USTcmd_cmsf* cmsf = NULL; | |
168 | unsigned int i = 0; | |
169 | int e = (opts->cmd == ENABLE_MARKER); | |
170 | ||
171 | if (opts->regex_state != 0) { | |
172 | return; | |
173 | } | |
174 | ||
175 | if (ustcmd_get_cmsf(&cmsf, pid)) { | |
176 | fprintf(stderr, "error while trying to get markers for PID " | |
177 | "%u\n", (unsigned int) pid); | |
178 | return; | |
179 | } | |
180 | while (cmsf[i].channel != NULL) { | |
181 | char* mc; | |
182 | asprintf(&mc, "%s/%s", cmsf[i].channel, cmsf[i].marker); | |
183 | if (regexec(&opts->preg, mc, 0, NULL, 0) == 0) { | |
184 | /* We got a match! */ | |
185 | if (ustcmd_set_marker_state(mc, | |
186 | e ? USTCMD_MS_ON : USTCMD_MS_OFF, pid)) { | |
187 | fprintf(stderr, | |
188 | "error while trying to %sable marker" | |
189 | "\"%s\" for PID %u\n", | |
190 | e ? "en" : "dis", mc, | |
191 | (unsigned int) pid); | |
192 | } else { | |
193 | printf("sucessfully %sabled marker " | |
194 | "\"%s\" for PID %u\n", | |
195 | e ? "en" : "dis", mc, | |
196 | (unsigned int) pid); | |
197 | } | |
198 | } | |
199 | free(mc); | |
200 | ++i; | |
201 | } | |
202 | ustcmd_free_cmsf(cmsf); | |
203 | } | |
204 | ||
fbd8191b PMF |
205 | int main(int argc, char *argv[]) |
206 | { | |
52c51a47 | 207 | pid_t *pidit; |
52c51a47 PMF |
208 | struct ustcomm_connection conn; |
209 | int result; | |
210 | struct ust_opts opts; | |
fbd8191b | 211 | |
52c51a47 | 212 | progname = argv[0]; |
fbd8191b | 213 | |
52c51a47 PMF |
214 | if(argc <= 1) { |
215 | fprintf(stderr, "No operation specified.\n"); | |
216 | usage(); | |
217 | exit(EXIT_FAILURE); | |
218 | } | |
219 | ||
220 | result = parse_opts_long(argc, argv, &opts); | |
221 | if(result) { | |
222 | usage(); | |
223 | exit(EXIT_FAILURE); | |
224 | } | |
225 | ||
ae937656 | 226 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
52c51a47 PMF |
227 | fprintf(stderr, "No pid specified.\n"); |
228 | usage(); | |
229 | exit(EXIT_FAILURE); | |
230 | } | |
ae937656 | 231 | if(opts.cmd == UNKNOWN) { |
52c51a47 PMF |
232 | fprintf(stderr, "No command specified.\n"); |
233 | usage(); | |
234 | exit(EXIT_FAILURE); | |
235 | } | |
ae937656 PP |
236 | if (opts.cmd == GET_ONLINE_PIDS) { |
237 | pid_t* pp = ustcmd_get_online_pids(); | |
238 | unsigned int i = 0; | |
52c51a47 | 239 | |
ae937656 PP |
240 | if (pp) { |
241 | while (pp[i] != 0) { | |
242 | printf("%u\n", (unsigned int) pp[i]); | |
243 | ++i; | |
244 | } | |
245 | free(pp); | |
52c51a47 | 246 | } |
ef290fca | 247 | |
ae937656 PP |
248 | exit(EXIT_SUCCESS); |
249 | } | |
52c51a47 | 250 | |
ae937656 PP |
251 | pidit = opts.pids; |
252 | struct USTcmd_cmsf* cmsf = NULL; | |
ef290fca | 253 | |
ae937656 PP |
254 | while(*pidit != -1) { |
255 | switch (opts.cmd) { | |
256 | case START_TRACE: | |
257 | if (ustcmd_start_trace(*pidit)) { | |
258 | fprintf(stderr, | |
259 | "error while trying to for trace " | |
260 | "with PID %u\n", (unsigned int) *pidit); | |
261 | break; | |
262 | } | |
263 | printf("sucessfully started trace for PID %u\n", | |
264 | (unsigned int) *pidit); | |
265 | break; | |
ef290fca | 266 | |
ae937656 PP |
267 | case STOP_TRACE: |
268 | if (ustcmd_stop_trace(*pidit)) { | |
269 | fprintf(stderr, | |
270 | "error while trying to stop trace " | |
271 | "for PID %u\n", (unsigned int) *pidit); | |
272 | break; | |
273 | } | |
274 | printf("sucessfully stopped trace for PID %u\n", | |
275 | (unsigned int) *pidit); | |
276 | break; | |
ef290fca | 277 | |
ae937656 PP |
278 | case START: |
279 | if (ustcmd_setup_and_start(*pidit)) { | |
280 | fprintf(stderr, | |
281 | "error while trying to setup/start " | |
282 | "trace for PID %u\n", | |
283 | (unsigned int) *pidit); | |
284 | break; | |
285 | } | |
286 | printf("sucessfully setup/started trace for PID %u\n", | |
287 | (unsigned int) *pidit); | |
288 | break; | |
ef290fca | 289 | |
ae937656 PP |
290 | case DESTROY: |
291 | if (ustcmd_destroy_trace(*pidit)) { | |
292 | fprintf(stderr, | |
293 | "error while trying to destroy " | |
294 | "trace with PID %u\n", | |
295 | (unsigned int) *pidit); | |
296 | break; | |
297 | } | |
298 | printf("sucessfully destroyed trace for PID %u\n", | |
299 | (unsigned int) *pidit); | |
300 | break; | |
ef290fca | 301 | |
ae937656 PP |
302 | case LIST_MARKERS: |
303 | cmsf = NULL; | |
304 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { | |
305 | fprintf(stderr, | |
306 | "error while trying to list markers for" | |
307 | " PID %u\n", (unsigned int) *pidit); | |
308 | break; | |
309 | } | |
310 | unsigned int i = 0; | |
311 | while (cmsf[i].channel != NULL) { | |
312 | printf("{PID: %u, channel/marker: %s/%s, " | |
313 | "state: %u, fs: %s}\n", | |
314 | (unsigned int) *pidit, | |
315 | cmsf[i].channel, | |
316 | cmsf[i].marker, | |
317 | cmsf[i].state, | |
318 | cmsf[i].fs); | |
319 | ++i; | |
320 | } | |
321 | ustcmd_free_cmsf(cmsf); | |
322 | break; | |
ef290fca | 323 | |
ae937656 | 324 | case ENABLE_MARKER: |
ae937656 | 325 | case DISABLE_MARKER: |
ef290fca | 326 | regex_change_m_state(&opts, *pidit); |
ae937656 | 327 | break; |
ef290fca | 328 | |
ae937656 PP |
329 | default: |
330 | fprintf(stderr, "error: unknown command...\n"); | |
331 | break; | |
332 | } | |
ef290fca | 333 | |
52c51a47 PMF |
334 | pidit++; |
335 | } | |
82b1a169 | 336 | |
ef290fca PMF |
337 | exit_free: |
338 | if (opts.pids != NULL) { | |
339 | free(opts.pids); | |
340 | } | |
341 | if (opts.regex != NULL) { | |
342 | free(opts.regex); | |
ae937656 | 343 | } |
fbd8191b PMF |
344 | |
345 | return 0; | |
346 | } | |
ae937656 | 347 |