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