2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
26 #include <sys/types.h>
30 #include <urcu/list.h>
32 #include <common/mi-lttng.h>
34 #include "../command.h"
41 static char *opt_session_name
;
42 static int opt_kernel
;
43 static int opt_userspace
;
45 static char *opt_pid_string
;
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, },
68 int parse_pid_string(const char *_pid_string
,
69 int all
, int **_pid_list
, int *nr_pids
)
71 const char *one_pid_str
;
73 int retval
= CMD_SUCCESS
;
76 char *pid_string
= NULL
;
79 if (all
&& _pid_string
) {
80 ERR("An empty PID string is expected with --all");
84 if (!all
&& !_pid_string
) {
85 ERR("Please specify --all with an empty PID string");
90 pid_list
= zmalloc(sizeof(*pid_list
));
96 /* Empty PID string means all PIDs */
102 pid_string
= strdup(_pid_string
);
104 ERR("Out of memory");
110 one_pid_str
= strtok_r(pid_string
, ",", &iter
);
111 while (one_pid_str
!= NULL
) {
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
);
124 if ((long) v
> INT_MAX
|| (int) v
< 0) {
125 ERR("Invalid PID value %ld", (long) v
);
132 one_pid_str
= strtok_r(NULL
, ",", &iter
);
136 /* Identity of delimiter has been lost in first pass. */
137 pid_string
= strdup(_pid_string
);
139 ERR("Out of memory");
145 pid_list
= zmalloc(count
* sizeof(*pid_list
));
147 ERR("Out of memory");
152 /* Reparse string and populate the pid list. */
154 one_pid_str
= strtok_r(pid_string
, ",", &iter
);
155 while (one_pid_str
!= NULL
) {
158 v
= strtoul(one_pid_str
, NULL
, 10);
159 pid_list
[count
++] = (int) v
;
162 one_pid_str
= strtok_r(NULL
, ",", &iter
);
167 *_pid_list
= pid_list
;
168 goto end
; /* SUCCESS */
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
)
183 int ret
, success
= 1 , i
;
184 enum cmd_error_code retval
= CMD_SUCCESS
;
185 int *pid_list
= NULL
;
187 struct lttng_domain dom
;
188 struct lttng_handle
*handle
= NULL
;
189 int (*cmd_func
)(struct lttng_handle
*handle
, int pid
);
193 cmd_func
= lttng_track_pid
;
196 cmd_func
= lttng_untrack_pid
;
199 ERR("Unknown command");
204 memset(&dom
, 0, sizeof(dom
));
206 dom
.type
= LTTNG_DOMAIN_KERNEL
;
207 } else if (opt_userspace
) {
208 dom
.type
= LTTNG_DOMAIN_UST
;
210 /* Checked by the caller. */
214 ret
= parse_pid_string(pid_string
, all
, &pid_list
, &nr_pids
);
215 if (ret
!= CMD_SUCCESS
) {
216 ERR("Error parsing PID string");
221 handle
= lttng_create_handle(session_name
, &dom
);
222 if (handle
== NULL
) {
228 /* Open process element */
229 ret
= mi_lttng_targets_open(writer
);
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
]);
241 case LTTNG_ERR_PID_TRACKED
:
242 WARN("PID %i already tracked in session %s",
243 pid_list
[i
], session_name
);
245 retval
= CMD_SUCCESS
;
247 case LTTNG_ERR_PID_NOT_TRACKED
:
248 WARN("PID %i not tracked in session %s",
249 pid_list
[i
], session_name
);
251 retval
= CMD_SUCCESS
;
254 ERR("%s", lttng_strerror(ret
));
260 if (pid_list
[i
] != -1) {
261 MSG("PID %i %sed in session %s",
262 pid_list
[i
], cmd_str
,
265 MSG("All PIDs %sed in session %s",
266 cmd_str
, session_name
);
273 ret
= mi_lttng_pid_target(writer
, pid_list
[i
], 1);
279 ret
= mi_lttng_writer_write_element_bool(writer
,
280 mi_lttng_element_success
, success
);
286 ret
= mi_lttng_writer_close_element(writer
);
295 /* Close targets element */
296 ret
= mi_lttng_writer_close_element(writer
);
305 lttng_destroy_handle(handle
);
312 const char *get_mi_element_command(enum cmd_type cmd_type
)
316 return mi_lttng_element_command_track
;
318 return mi_lttng_element_command_untrack
;
325 * Add/remove tracker to/from session.
328 int cmd_track_untrack(enum cmd_type cmd_type
, const char *cmd_str
,
329 int argc
, const char **argv
, const char *help_msg
)
332 enum cmd_error_code command_ret
= CMD_SUCCESS
;
334 static poptContext pc
;
335 char *session_name
= NULL
;
336 struct mi_writer
*writer
= NULL
;
339 command_ret
= CMD_ERROR
;
343 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
344 poptReadDefaultConfig(pc
, 0);
346 while ((opt
= poptGetNextOpt(pc
)) != -1) {
351 case OPT_LIST_OPTIONS
:
352 list_cmd_options(stdout
, long_options
);
359 command_ret
= CMD_UNDEFINED
;
364 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
366 command_ret
= CMD_ERROR
;
370 if (!opt_session_name
) {
371 session_name
= get_session_name();
372 if (session_name
== NULL
) {
373 command_ret
= CMD_ERROR
;
377 session_name
= opt_session_name
;
380 /* Currently only PID tracker is supported */
382 ERR("Please specify at least one tracker with its expected arguments");
383 command_ret
= CMD_ERROR
;
389 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
391 command_ret
= CMD_ERROR
;
397 /* Open command element */
398 ret
= mi_lttng_writer_command_open(writer
,
399 get_mi_element_command(cmd_type
));
401 command_ret
= CMD_ERROR
;
405 /* Open output element */
406 ret
= mi_lttng_writer_open_element(writer
,
407 mi_lttng_element_command_output
);
409 command_ret
= CMD_ERROR
;
414 command_ret
= track_untrack_pid(cmd_type
,
415 cmd_str
, session_name
, opt_pid_string
,
417 if (command_ret
!= CMD_SUCCESS
) {
423 /* Close output element */
424 ret
= mi_lttng_writer_close_element(writer
);
426 command_ret
= CMD_ERROR
;
431 ret
= mi_lttng_writer_write_element_bool(writer
,
432 mi_lttng_element_command_success
, success
);
434 command_ret
= CMD_ERROR
;
438 /* Command element close */
439 ret
= mi_lttng_writer_command_close(writer
);
441 command_ret
= CMD_ERROR
;
447 if (!opt_session_name
) {
452 if (writer
&& mi_lttng_writer_destroy(writer
)) {
453 /* Preserve original error code */
454 command_ret
= CMD_ERROR
;
458 return (int) command_ret
;
461 int cmd_track(int argc
, const char **argv
)
463 static const char *help_msg
=
464 #ifdef LTTNG_EMBED_HELP
465 #include <lttng-track.1.h>
471 return cmd_track_untrack(CMD_TRACK
, "track", argc
, argv
, help_msg
);
474 int cmd_untrack(int argc
, const char **argv
)
476 static const char *help_msg
=
477 #ifdef LTTNG_EMBED_HELP
478 #include <lttng-untrack.1.h>
484 return cmd_track_untrack(CMD_UNTRACK
, "untrack", argc
, argv
, help_msg
);