Commit | Line | Data |
---|---|---|
ccf10263 MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #define _LGPL_SOURCE | |
21 | #include <ctype.h> | |
22 | #include <popt.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <unistd.h> | |
29 | ||
30 | #include <urcu/list.h> | |
31 | ||
32 | #include <common/mi-lttng.h> | |
33 | ||
34 | #include "../command.h" | |
35 | ||
36 | enum cmd_type { | |
37 | CMD_TRACK, | |
38 | CMD_UNTRACK, | |
39 | }; | |
40 | ||
41 | static char *opt_session_name; | |
42 | static int opt_kernel; | |
43 | static int opt_userspace; | |
44 | static int opt_all; | |
45 | static char *opt_pid_string; | |
46 | static int opt_pid; | |
47 | ||
48 | enum { | |
49 | OPT_HELP = 1, | |
50 | OPT_LIST_OPTIONS, | |
51 | OPT_SESSION, | |
52 | OPT_PID, | |
53 | }; | |
54 | ||
55 | static struct poptOption long_options[] = { | |
56 | /* { longName, shortName, argInfo, argPtr, value, descrip, argDesc, } */ | |
57 | { "help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0, }, | |
58 | { "session", 's', POPT_ARG_STRING, &opt_session_name, OPT_SESSION, 0, 0, }, | |
59 | { "kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0, }, | |
60 | { "userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0, }, | |
61 | { "pid", 'p', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_pid_string, OPT_PID, 0, 0, }, | |
62 | { "all", 'a', POPT_ARG_VAL, &opt_all, 1, 0, 0, }, | |
63 | { "list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, 0, 0, }, | |
64 | { 0, 0, 0, 0, 0, 0, 0, }, | |
65 | }; | |
66 | ||
67 | /* | |
68 | * usage | |
69 | */ | |
70 | static void usage(FILE *ofp, const char *cmd_str) | |
71 | { | |
72 | fprintf(ofp, "usage: lttng %s [-k|-u] [OPTIONS]\n", cmd_str); | |
73 | fprintf(ofp, "\n"); | |
74 | fprintf(ofp, "If no session is given (-s), the context is added to\n"); | |
75 | fprintf(ofp, "the current sesssion. Exactly one domain (-k or -u)\n"); | |
76 | fprintf(ofp, "must be specified.\n"); | |
77 | fprintf(ofp, "\n"); | |
78 | fprintf(ofp, "Options:\n"); | |
79 | fprintf(ofp, " -h, --help Show this help.\n"); | |
80 | fprintf(ofp, " --list-options Simple listing of options.\n"); | |
81 | fprintf(ofp, " -s, --session NAME Apply to session name.\n"); | |
82 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer.\n"); | |
83 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer.\n"); | |
84 | fprintf(ofp, " -p, --pid [PID] Process ID tracker. Leave PID empty when used with --all.\n"); | |
85 | fprintf(ofp, " -a, --all All PIDs (use with --pid).\n"); | |
86 | fprintf(ofp, "\n"); | |
87 | } | |
88 | ||
89 | static | |
90 | int parse_pid_string(const char *_pid_string, | |
91 | int all, int **_pid_list, int *nr_pids) | |
92 | { | |
93 | const char *one_pid_str; | |
94 | char *iter; | |
95 | int retval = CMD_SUCCESS; | |
96 | int count = 0; | |
97 | int *pid_list = NULL; | |
98 | char *pid_string = NULL; | |
44c1a903 | 99 | char *endptr; |
ccf10263 MD |
100 | |
101 | if (all && _pid_string) { | |
102 | ERR("An empty PID string is expected with --all"); | |
103 | retval = CMD_ERROR; | |
104 | goto error; | |
105 | } | |
106 | if (!all && !_pid_string) { | |
107 | ERR("Please specify --all with an empty PID string"); | |
108 | retval = CMD_ERROR; | |
109 | goto error; | |
110 | } | |
111 | if (all) { | |
112 | pid_list = zmalloc(sizeof(*_pid_list)); | |
113 | if (!pid_list) { | |
114 | ERR("Out of memory"); | |
115 | retval = CMD_ERROR; | |
116 | goto error; | |
117 | } | |
118 | /* Empty PID string means all PIDs */ | |
119 | count = 1; | |
120 | pid_list[0] = -1; | |
121 | goto assign; | |
122 | } | |
123 | ||
124 | pid_string = strdup(_pid_string); | |
125 | if (!pid_string) { | |
126 | ERR("Out of memory"); | |
127 | retval = CMD_ERROR; | |
128 | goto error; | |
129 | } | |
130 | ||
131 | /* Count */ | |
132 | one_pid_str = strtok_r(pid_string, ",", &iter); | |
133 | while (one_pid_str != NULL) { | |
134 | unsigned long v; | |
135 | ||
44c1a903 JR |
136 | errno = 0; |
137 | v = strtoul(one_pid_str, &endptr, 10); | |
ccf10263 | 138 | if ((v == 0 && errno == EINVAL) |
44c1a903 JR |
139 | || (v == ULONG_MAX && errno == ERANGE) |
140 | || (*one_pid_str != '\0' && *endptr != '\0')){ | |
ccf10263 MD |
141 | ERR("Error parsing PID %s", one_pid_str); |
142 | retval = CMD_ERROR; | |
143 | goto error; | |
144 | } | |
44c1a903 | 145 | |
ccf10263 MD |
146 | if ((long) v > INT_MAX || (int) v < 0) { |
147 | ERR("Invalid PID value %ld", (long) v); | |
148 | retval = CMD_ERROR; | |
149 | goto error; | |
150 | } | |
151 | count++; | |
152 | ||
153 | /* For next loop */ | |
154 | one_pid_str = strtok_r(NULL, ",", &iter); | |
155 | } | |
156 | ||
157 | free(pid_string); | |
158 | /* Identity of delimiter has been lost in first pass. */ | |
159 | pid_string = strdup(_pid_string); | |
160 | if (!pid_string) { | |
161 | ERR("Out of memory"); | |
162 | retval = CMD_ERROR; | |
163 | goto error; | |
164 | } | |
165 | ||
166 | /* Allocate */ | |
167 | pid_list = zmalloc(count * sizeof(*pid_list)); | |
168 | if (!pid_list) { | |
169 | ERR("Out of memory"); | |
170 | retval = CMD_ERROR; | |
171 | goto error; | |
172 | } | |
173 | ||
174 | /* Copy */ | |
175 | count = 0; | |
176 | one_pid_str = strtok_r(pid_string, ",", &iter); | |
177 | while (one_pid_str != NULL) { | |
178 | unsigned long v; | |
179 | ||
180 | v = strtoul(one_pid_str, NULL, 10); | |
181 | pid_list[count++] = (int) v; | |
182 | ||
183 | /* For next loop */ | |
184 | one_pid_str = strtok_r(NULL, ",", &iter); | |
185 | } | |
186 | ||
187 | assign: | |
188 | *nr_pids = count; | |
189 | *_pid_list = pid_list; | |
190 | goto end; /* SUCCESS */ | |
191 | ||
192 | /* ERROR */ | |
193 | error: | |
194 | free(pid_list); | |
195 | end: | |
196 | free(pid_string); | |
197 | return retval; | |
198 | } | |
199 | ||
200 | static | |
201 | int track_untrack_pid(enum cmd_type cmd_type, const char *cmd_str, | |
202 | const char *session_name, const char *pid_string, | |
203 | int all, struct mi_writer *writer) | |
204 | { | |
205 | int ret, retval = CMD_SUCCESS, i; | |
206 | int *pid_list = NULL; | |
207 | int nr_pids; | |
208 | struct lttng_domain dom; | |
209 | struct lttng_handle *handle = NULL; | |
210 | int (*lib_func)(struct lttng_handle *handle, int pid); | |
211 | ||
212 | switch (cmd_type) { | |
213 | case CMD_TRACK: | |
214 | lib_func = lttng_track_pid; | |
215 | break; | |
216 | case CMD_UNTRACK: | |
217 | lib_func = lttng_untrack_pid; | |
218 | break; | |
219 | default: | |
220 | ERR("Unknown command"); | |
221 | retval = CMD_ERROR; | |
222 | goto end; | |
223 | } | |
224 | ||
225 | memset(&dom, 0, sizeof(dom)); | |
226 | if (opt_kernel) { | |
227 | dom.type = LTTNG_DOMAIN_KERNEL; | |
228 | } else if (opt_userspace) { | |
229 | dom.type = LTTNG_DOMAIN_UST; | |
230 | } else { | |
231 | print_missing_domain(); | |
232 | ret = CMD_ERROR; | |
233 | goto end; | |
234 | } | |
235 | ||
236 | ret = parse_pid_string(pid_string, all, &pid_list, &nr_pids); | |
237 | if (ret != CMD_SUCCESS) { | |
238 | ERR("Error parsing PID string"); | |
239 | usage(stderr, cmd_str); | |
240 | retval = CMD_ERROR; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | handle = lttng_create_handle(session_name, &dom); | |
245 | if (handle == NULL) { | |
246 | retval = CMD_ERROR; | |
247 | goto end; | |
248 | } | |
249 | ||
250 | if (writer) { | |
251 | /* Open pids element */ | |
252 | ret = mi_lttng_writer_open_element(writer, config_element_pids); | |
253 | if (ret) { | |
254 | retval = CMD_ERROR; | |
255 | goto end; | |
256 | } | |
257 | } | |
258 | ||
259 | /* TODO: MI */ | |
260 | for (i = 0; i < nr_pids; i++) { | |
261 | DBG("%s PID %d", cmd_str, pid_list[i]); | |
262 | ret = lib_func(handle, pid_list[i]); | |
263 | if (ret) { | |
264 | retval = CMD_ERROR; | |
265 | goto end; | |
266 | } | |
267 | } | |
268 | ||
269 | if (writer) { | |
270 | /* Close pids element */ | |
271 | ret = mi_lttng_writer_close_element(writer); | |
272 | if (ret) { | |
273 | retval = CMD_ERROR; | |
274 | goto end; | |
275 | } | |
276 | } | |
277 | ||
278 | /* SUCCESS */ | |
279 | end: | |
280 | if (handle) { | |
281 | lttng_destroy_handle(handle); | |
282 | } | |
283 | free(pid_list); | |
284 | return retval; | |
285 | } | |
286 | ||
287 | static | |
288 | const char *get_mi_element_command(enum cmd_type cmd_type) | |
289 | { | |
290 | switch (cmd_type) { | |
291 | case CMD_TRACK: | |
292 | return mi_lttng_element_command_track; | |
293 | case CMD_UNTRACK: | |
294 | return mi_lttng_element_command_untrack; | |
295 | default: | |
296 | return NULL; | |
297 | } | |
298 | } | |
299 | ||
300 | /* | |
301 | * Add/remove tracker to/from session. | |
302 | */ | |
303 | static | |
304 | int cmd_track_untrack(enum cmd_type cmd_type, const char *cmd_str, | |
305 | int argc, const char **argv) | |
306 | { | |
307 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; | |
308 | int success = 1; | |
309 | static poptContext pc; | |
310 | char *session_name = NULL; | |
311 | struct mi_writer *writer = NULL; | |
312 | ||
313 | if (argc < 1) { | |
314 | usage(stderr, cmd_str); | |
315 | ret = CMD_ERROR; | |
316 | goto end; | |
317 | } | |
318 | ||
319 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
320 | poptReadDefaultConfig(pc, 0); | |
321 | ||
322 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
323 | switch (opt) { | |
324 | case OPT_HELP: | |
325 | usage(stdout, cmd_str); | |
326 | goto end; | |
327 | case OPT_LIST_OPTIONS: | |
328 | list_cmd_options(stdout, long_options); | |
329 | goto end; | |
330 | case OPT_SESSION: | |
331 | case OPT_PID: | |
332 | opt_pid = 1; | |
333 | break; | |
334 | default: | |
335 | usage(stderr, cmd_str); | |
336 | ret = CMD_UNDEFINED; | |
337 | goto end; | |
338 | } | |
339 | } | |
340 | ||
341 | if (!(opt_userspace ^ opt_kernel)) { | |
342 | ERR("Exactly one of -u or -k needs to be specified."); | |
343 | usage(stderr, cmd_str); | |
344 | ret = CMD_ERROR; | |
345 | goto end; | |
346 | } | |
347 | ||
348 | if (!opt_session_name) { | |
349 | session_name = get_session_name(); | |
350 | if (session_name == NULL) { | |
351 | ret = CMD_ERROR; | |
352 | goto end; | |
353 | } | |
354 | } else { | |
355 | session_name = opt_session_name; | |
356 | } | |
357 | ||
358 | /* Currently only PID tracker is supported */ | |
359 | if (!opt_pid) { | |
360 | ERR("Please specify at least one tracker with its expected arguments"); | |
361 | usage(stderr, cmd_str); | |
362 | ret = CMD_ERROR; | |
363 | goto end; | |
364 | } | |
365 | ||
366 | /* Mi check */ | |
367 | if (lttng_opt_mi) { | |
368 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
369 | if (!writer) { | |
370 | ret = CMD_ERROR; | |
371 | goto end; | |
372 | } | |
373 | } | |
374 | ||
375 | if (writer) { | |
376 | /* Open command element */ | |
377 | ret = mi_lttng_writer_command_open(writer, | |
378 | get_mi_element_command(cmd_type)); | |
379 | if (ret) { | |
380 | ret = CMD_ERROR; | |
381 | goto end; | |
382 | } | |
383 | ||
384 | /* Open output element */ | |
385 | ret = mi_lttng_writer_open_element(writer, | |
386 | mi_lttng_element_command_output); | |
387 | if (ret) { | |
388 | ret = CMD_ERROR; | |
389 | goto end; | |
390 | } | |
391 | } | |
392 | ||
393 | command_ret = track_untrack_pid(cmd_type, | |
394 | cmd_str, session_name, opt_pid_string, | |
395 | opt_all, writer); | |
396 | if (command_ret) { | |
397 | success = 0; | |
398 | } | |
399 | ||
400 | /* Mi closing */ | |
401 | if (writer) { | |
402 | /* Close output element */ | |
403 | ret = mi_lttng_writer_close_element(writer); | |
404 | if (ret) { | |
405 | ret = CMD_ERROR; | |
406 | goto end; | |
407 | } | |
408 | ||
409 | /* Success ? */ | |
410 | ret = mi_lttng_writer_write_element_bool(writer, | |
411 | mi_lttng_element_command_success, success); | |
412 | if (ret) { | |
413 | ret = CMD_ERROR; | |
414 | goto end; | |
415 | } | |
416 | ||
417 | /* Command element close */ | |
418 | ret = mi_lttng_writer_command_close(writer); | |
419 | if (ret) { | |
420 | ret = CMD_ERROR; | |
421 | goto end; | |
422 | } | |
423 | } | |
424 | ||
425 | end: | |
426 | if (!opt_session_name) { | |
427 | free(session_name); | |
428 | } | |
429 | ||
430 | /* Mi clean-up */ | |
431 | if (writer && mi_lttng_writer_destroy(writer)) { | |
432 | /* Preserve original error code */ | |
433 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
434 | } | |
435 | ||
436 | /* Overwrite ret if an error occurred during track() */ | |
437 | ret = command_ret ? command_ret : ret; | |
438 | ||
439 | poptFreeContext(pc); | |
440 | return ret; | |
441 | } | |
442 | ||
443 | int cmd_track(int argc, const char **argv) | |
444 | { | |
445 | return cmd_track_untrack(CMD_TRACK, "track", argc, argv); | |
446 | } | |
447 | ||
448 | int cmd_untrack(int argc, const char **argv) | |
449 | { | |
450 | return cmd_track_untrack(CMD_UNTRACK, "untrack", argc, argv); | |
451 | } |