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
);
135 ERR("Fatal error occurred when parsing pid string");
141 /* Identity of delimiter has been lost in first pass. */
142 pid_string
= strdup(_pid_string
);
144 ERR("Out of memory");
150 pid_list
= zmalloc(count
* sizeof(*pid_list
));
152 ERR("Out of memory");
157 /* Reparse string and populate the pid list. */
159 one_pid_str
= strtok_r(pid_string
, ",", &iter
);
160 while (one_pid_str
!= NULL
) {
163 v
= strtoul(one_pid_str
, NULL
, 10);
164 pid_list
[count
++] = (int) v
;
167 one_pid_str
= strtok_r(NULL
, ",", &iter
);
172 *_pid_list
= pid_list
;
173 goto end
; /* SUCCESS */
184 enum cmd_error_code
track_untrack_pid(enum cmd_type cmd_type
, const char *cmd_str
,
185 const char *session_name
, const char *pid_string
,
186 int all
, struct mi_writer
*writer
)
188 int ret
, success
= 1 , i
;
189 enum cmd_error_code retval
= CMD_SUCCESS
;
190 int *pid_list
= NULL
;
192 struct lttng_domain dom
;
193 struct lttng_handle
*handle
= NULL
;
194 int (*cmd_func
)(struct lttng_handle
*handle
, int pid
);
198 cmd_func
= lttng_track_pid
;
201 cmd_func
= lttng_untrack_pid
;
204 ERR("Unknown command");
209 memset(&dom
, 0, sizeof(dom
));
211 dom
.type
= LTTNG_DOMAIN_KERNEL
;
212 } else if (opt_userspace
) {
213 dom
.type
= LTTNG_DOMAIN_UST
;
215 /* Checked by the caller. */
219 ret
= parse_pid_string(pid_string
, all
, &pid_list
, &nr_pids
);
220 if (ret
!= CMD_SUCCESS
) {
221 ERR("Error parsing PID string");
226 handle
= lttng_create_handle(session_name
, &dom
);
227 if (handle
== NULL
) {
233 /* Open process element */
234 ret
= mi_lttng_targets_open(writer
);
241 for (i
= 0; i
< nr_pids
; i
++) {
242 DBG("%s PID %d", cmd_str
, pid_list
[i
]);
243 ret
= cmd_func(handle
, pid_list
[i
]);
246 case LTTNG_ERR_PID_TRACKED
:
247 WARN("PID %i already tracked in session %s",
248 pid_list
[i
], session_name
);
250 retval
= CMD_SUCCESS
;
252 case LTTNG_ERR_PID_NOT_TRACKED
:
253 WARN("PID %i not tracked in session %s",
254 pid_list
[i
], session_name
);
256 retval
= CMD_SUCCESS
;
259 ERR("%s", lttng_strerror(ret
));
265 if (pid_list
[i
] != -1) {
266 MSG("PID %i %sed in session %s",
267 pid_list
[i
], cmd_str
,
270 MSG("All PIDs %sed in session %s",
271 cmd_str
, session_name
);
278 ret
= mi_lttng_pid_target(writer
, pid_list
[i
], 1);
284 ret
= mi_lttng_writer_write_element_bool(writer
,
285 mi_lttng_element_success
, success
);
291 ret
= mi_lttng_writer_close_element(writer
);
300 /* Close targets element */
301 ret
= mi_lttng_writer_close_element(writer
);
310 lttng_destroy_handle(handle
);
317 const char *get_mi_element_command(enum cmd_type cmd_type
)
321 return mi_lttng_element_command_track
;
323 return mi_lttng_element_command_untrack
;
330 * Add/remove tracker to/from session.
333 int cmd_track_untrack(enum cmd_type cmd_type
, const char *cmd_str
,
334 int argc
, const char **argv
, const char *help_msg
)
337 enum cmd_error_code command_ret
= CMD_SUCCESS
;
339 static poptContext pc
;
340 char *session_name
= NULL
;
341 struct mi_writer
*writer
= NULL
;
344 command_ret
= CMD_ERROR
;
348 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
349 poptReadDefaultConfig(pc
, 0);
351 while ((opt
= poptGetNextOpt(pc
)) != -1) {
356 case OPT_LIST_OPTIONS
:
357 list_cmd_options(stdout
, long_options
);
364 command_ret
= CMD_UNDEFINED
;
369 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
371 command_ret
= CMD_ERROR
;
375 if (!opt_session_name
) {
376 session_name
= get_session_name();
377 if (session_name
== NULL
) {
378 command_ret
= CMD_ERROR
;
382 session_name
= opt_session_name
;
385 /* Currently only PID tracker is supported */
387 ERR("Please specify at least one tracker with its expected arguments");
388 command_ret
= CMD_ERROR
;
394 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
396 command_ret
= CMD_ERROR
;
402 /* Open command element */
403 ret
= mi_lttng_writer_command_open(writer
,
404 get_mi_element_command(cmd_type
));
406 command_ret
= CMD_ERROR
;
410 /* Open output element */
411 ret
= mi_lttng_writer_open_element(writer
,
412 mi_lttng_element_command_output
);
414 command_ret
= CMD_ERROR
;
419 command_ret
= track_untrack_pid(cmd_type
,
420 cmd_str
, session_name
, opt_pid_string
,
422 if (command_ret
!= CMD_SUCCESS
) {
428 /* Close output element */
429 ret
= mi_lttng_writer_close_element(writer
);
431 command_ret
= CMD_ERROR
;
436 ret
= mi_lttng_writer_write_element_bool(writer
,
437 mi_lttng_element_command_success
, success
);
439 command_ret
= CMD_ERROR
;
443 /* Command element close */
444 ret
= mi_lttng_writer_command_close(writer
);
446 command_ret
= CMD_ERROR
;
452 if (!opt_session_name
) {
457 if (writer
&& mi_lttng_writer_destroy(writer
)) {
458 /* Preserve original error code */
459 command_ret
= CMD_ERROR
;
463 return (int) command_ret
;
466 int cmd_track(int argc
, const char **argv
)
468 static const char *help_msg
=
469 #ifdef LTTNG_EMBED_HELP
470 #include <lttng-track.1.h>
476 return cmd_track_untrack(CMD_TRACK
, "track", argc
, argv
, help_msg
);
479 int cmd_untrack(int argc
, const char **argv
)
481 static const char *help_msg
=
482 #ifdef LTTNG_EMBED_HELP
483 #include <lttng-untrack.1.h>
489 return cmd_track_untrack(CMD_UNTRACK
, "untrack", argc
, argv
, help_msg
);