6db4288c59ec471cf181a8429dfa424a0ff75762
[lttng-tools.git] / src / bin / lttng / commands / track-untrack.c
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 _LGPL_SOURCE
20 #include <ctype.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <assert.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 static
68 int parse_pid_string(const char *_pid_string,
69 int all, int **_pid_list, int *nr_pids)
70 {
71 const char *one_pid_str;
72 char *iter;
73 int retval = CMD_SUCCESS;
74 int count = 0;
75 int *pid_list = NULL;
76 char *pid_string = NULL;
77 char *endptr;
78
79 if (all && _pid_string) {
80 ERR("An empty PID string is expected with --all");
81 retval = CMD_ERROR;
82 goto error;
83 }
84 if (!all && !_pid_string) {
85 ERR("Please specify --all with an empty PID string");
86 retval = CMD_ERROR;
87 goto error;
88 }
89 if (all) {
90 pid_list = zmalloc(sizeof(*pid_list));
91 if (!pid_list) {
92 ERR("Out of memory");
93 retval = CMD_ERROR;
94 goto error;
95 }
96 /* Empty PID string means all PIDs */
97 count = 1;
98 pid_list[0] = -1;
99 goto assign;
100 }
101
102 pid_string = strdup(_pid_string);
103 if (!pid_string) {
104 ERR("Out of memory");
105 retval = CMD_ERROR;
106 goto error;
107 }
108
109 /* Count */
110 one_pid_str = strtok_r(pid_string, ",", &iter);
111 while (one_pid_str != NULL) {
112 unsigned long v;
113
114 errno = 0;
115 v = strtoul(one_pid_str, &endptr, 10);
116 if ((v == 0 && errno == EINVAL)
117 || (v == ULONG_MAX && errno == ERANGE)
118 || (*one_pid_str != '\0' && *endptr != '\0')){
119 ERR("Error parsing PID %s", one_pid_str);
120 retval = CMD_ERROR;
121 goto error;
122 }
123
124 if ((long) v > INT_MAX || (int) v < 0) {
125 ERR("Invalid PID value %ld", (long) v);
126 retval = CMD_ERROR;
127 goto error;
128 }
129 count++;
130
131 /* For next loop */
132 one_pid_str = strtok_r(NULL, ",", &iter);
133 }
134
135 free(pid_string);
136 /* Identity of delimiter has been lost in first pass. */
137 pid_string = strdup(_pid_string);
138 if (!pid_string) {
139 ERR("Out of memory");
140 retval = CMD_ERROR;
141 goto error;
142 }
143
144 /* Allocate */
145 pid_list = zmalloc(count * sizeof(*pid_list));
146 if (!pid_list) {
147 ERR("Out of memory");
148 retval = CMD_ERROR;
149 goto error;
150 }
151
152 /* Reparse string and populate the pid list. */
153 count = 0;
154 one_pid_str = strtok_r(pid_string, ",", &iter);
155 while (one_pid_str != NULL) {
156 unsigned long v;
157
158 v = strtoul(one_pid_str, NULL, 10);
159 pid_list[count++] = (int) v;
160
161 /* For next loop */
162 one_pid_str = strtok_r(NULL, ",", &iter);
163 }
164
165 assign:
166 *nr_pids = count;
167 *_pid_list = pid_list;
168 goto end; /* SUCCESS */
169
170 /* ERROR */
171 error:
172 free(pid_list);
173 end:
174 free(pid_string);
175 return retval;
176 }
177
178 static
179 enum cmd_error_code track_untrack_pid(enum cmd_type cmd_type, const char *cmd_str,
180 const char *session_name, const char *pid_string,
181 int all, struct mi_writer *writer)
182 {
183 int ret, success = 1 , i;
184 enum cmd_error_code retval = CMD_SUCCESS;
185 int *pid_list = NULL;
186 int nr_pids;
187 struct lttng_domain dom;
188 struct lttng_handle *handle = NULL;
189 int (*cmd_func)(struct lttng_handle *handle, int pid);
190
191 switch (cmd_type) {
192 case CMD_TRACK:
193 cmd_func = lttng_track_pid;
194 break;
195 case CMD_UNTRACK:
196 cmd_func = lttng_untrack_pid;
197 break;
198 default:
199 ERR("Unknown command");
200 retval = CMD_ERROR;
201 goto end;
202 }
203
204 memset(&dom, 0, sizeof(dom));
205 if (opt_kernel) {
206 dom.type = LTTNG_DOMAIN_KERNEL;
207 } else if (opt_userspace) {
208 dom.type = LTTNG_DOMAIN_UST;
209 } else {
210 /* Checked by the caller. */
211 assert(0);
212 }
213
214 ret = parse_pid_string(pid_string, all, &pid_list, &nr_pids);
215 if (ret != CMD_SUCCESS) {
216 ERR("Error parsing PID string");
217 retval = CMD_ERROR;
218 goto end;
219 }
220
221 handle = lttng_create_handle(session_name, &dom);
222 if (handle == NULL) {
223 retval = CMD_ERROR;
224 goto end;
225 }
226
227 if (writer) {
228 /* Open process element */
229 ret = mi_lttng_targets_open(writer);
230 if (ret) {
231 retval = CMD_ERROR;
232 goto end;
233 }
234 }
235
236 for (i = 0; i < nr_pids; i++) {
237 DBG("%s PID %d", cmd_str, pid_list[i]);
238 ret = cmd_func(handle, pid_list[i]);
239 if (ret) {
240 switch (-ret) {
241 case LTTNG_ERR_PID_TRACKED:
242 WARN("PID %i already tracked in session %s",
243 pid_list[i], session_name);
244 success = 1;
245 retval = CMD_SUCCESS;
246 break;
247 case LTTNG_ERR_PID_NOT_TRACKED:
248 WARN("PID %i not tracked in session %s",
249 pid_list[i], session_name);
250 success = 1;
251 retval = CMD_SUCCESS;
252 break;
253 default:
254 ERR("%s", lttng_strerror(ret));
255 success = 0;
256 retval = CMD_ERROR;
257 break;
258 }
259 } else {
260 if (pid_list[i] != -1) {
261 MSG("PID %i %sed in session %s",
262 pid_list[i], cmd_str,
263 session_name);
264 } else {
265 MSG("All PIDs %sed in session %s",
266 cmd_str, session_name);
267 }
268 success = 1;
269 }
270
271 /* Mi */
272 if (writer) {
273 ret = mi_lttng_pid_target(writer, pid_list[i], 1);
274 if (ret) {
275 retval = CMD_ERROR;
276 goto end;
277 }
278
279 ret = mi_lttng_writer_write_element_bool(writer,
280 mi_lttng_element_success, success);
281 if (ret) {
282 retval = CMD_ERROR;
283 goto end;
284 }
285
286 ret = mi_lttng_writer_close_element(writer);
287 if (ret) {
288 retval = CMD_ERROR;
289 goto end;
290 }
291 }
292 }
293
294 if (writer) {
295 /* Close targets element */
296 ret = mi_lttng_writer_close_element(writer);
297 if (ret) {
298 retval = CMD_ERROR;
299 goto end;
300 }
301 }
302
303 end:
304 if (handle) {
305 lttng_destroy_handle(handle);
306 }
307 free(pid_list);
308 return retval;
309 }
310
311 static
312 const char *get_mi_element_command(enum cmd_type cmd_type)
313 {
314 switch (cmd_type) {
315 case CMD_TRACK:
316 return mi_lttng_element_command_track;
317 case CMD_UNTRACK:
318 return mi_lttng_element_command_untrack;
319 default:
320 return NULL;
321 }
322 }
323
324 /*
325 * Add/remove tracker to/from session.
326 */
327 static
328 int cmd_track_untrack(enum cmd_type cmd_type, const char *cmd_str,
329 int argc, const char **argv, const char *help_msg)
330 {
331 int opt, ret = 0;
332 enum cmd_error_code command_ret = CMD_SUCCESS;
333 int success = 1;
334 static poptContext pc;
335 char *session_name = NULL;
336 struct mi_writer *writer = NULL;
337
338 if (argc < 1) {
339 command_ret = CMD_ERROR;
340 goto end;
341 }
342
343 pc = poptGetContext(NULL, argc, argv, long_options, 0);
344 poptReadDefaultConfig(pc, 0);
345
346 while ((opt = poptGetNextOpt(pc)) != -1) {
347 switch (opt) {
348 case OPT_HELP:
349 SHOW_HELP();
350 goto end;
351 case OPT_LIST_OPTIONS:
352 list_cmd_options(stdout, long_options);
353 goto end;
354 case OPT_SESSION:
355 case OPT_PID:
356 opt_pid = 1;
357 break;
358 default:
359 command_ret = CMD_UNDEFINED;
360 goto end;
361 }
362 }
363
364 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
365 if (ret) {
366 command_ret = CMD_ERROR;
367 goto end;
368 }
369
370 if (!opt_session_name) {
371 session_name = get_session_name();
372 if (session_name == NULL) {
373 command_ret = CMD_ERROR;
374 goto end;
375 }
376 } else {
377 session_name = opt_session_name;
378 }
379
380 /* Currently only PID tracker is supported */
381 if (!opt_pid) {
382 ERR("Please specify at least one tracker with its expected arguments");
383 command_ret = CMD_ERROR;
384 goto end;
385 }
386
387 /* Mi check */
388 if (lttng_opt_mi) {
389 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
390 if (!writer) {
391 command_ret = CMD_ERROR;
392 goto end;
393 }
394 }
395
396 if (writer) {
397 /* Open command element */
398 ret = mi_lttng_writer_command_open(writer,
399 get_mi_element_command(cmd_type));
400 if (ret) {
401 command_ret = CMD_ERROR;
402 goto end;
403 }
404
405 /* Open output element */
406 ret = mi_lttng_writer_open_element(writer,
407 mi_lttng_element_command_output);
408 if (ret) {
409 command_ret = CMD_ERROR;
410 goto end;
411 }
412 }
413
414 command_ret = track_untrack_pid(cmd_type,
415 cmd_str, session_name, opt_pid_string,
416 opt_all, writer);
417 if (command_ret != CMD_SUCCESS) {
418 success = 0;
419 }
420
421 /* Mi closing */
422 if (writer) {
423 /* Close output element */
424 ret = mi_lttng_writer_close_element(writer);
425 if (ret) {
426 command_ret = CMD_ERROR;
427 goto end;
428 }
429
430 /* Success ? */
431 ret = mi_lttng_writer_write_element_bool(writer,
432 mi_lttng_element_command_success, success);
433 if (ret) {
434 command_ret = CMD_ERROR;
435 goto end;
436 }
437
438 /* Command element close */
439 ret = mi_lttng_writer_command_close(writer);
440 if (ret) {
441 command_ret = CMD_ERROR;
442 goto end;
443 }
444 }
445
446 end:
447 if (!opt_session_name) {
448 free(session_name);
449 }
450
451 /* Mi clean-up */
452 if (writer && mi_lttng_writer_destroy(writer)) {
453 /* Preserve original error code */
454 command_ret = CMD_ERROR;
455 }
456
457 poptFreeContext(pc);
458 return (int) command_ret;
459 }
460
461 int cmd_track(int argc, const char **argv)
462 {
463 static const char *help_msg =
464 #ifdef LTTNG_EMBED_HELP
465 #include <lttng-track.1.h>
466 #else
467 NULL
468 #endif
469 ;
470
471 return cmd_track_untrack(CMD_TRACK, "track", argc, argv, help_msg);
472 }
473
474 int cmd_untrack(int argc, const char **argv)
475 {
476 static const char *help_msg =
477 #ifdef LTTNG_EMBED_HELP
478 #include <lttng-untrack.1.h>
479 #else
480 NULL
481 #endif
482 ;
483
484 return cmd_track_untrack(CMD_UNTRACK, "untrack", argc, argv, help_msg);
485 }
This page took 0.037613 seconds and 3 git commands to generate.