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.
27 #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, },
70 static void usage(FILE *ofp
, const char *cmd_str
)
72 fprintf(ofp
, "usage: lttng %s [-k|-u] [OPTIONS]\n", cmd_str
);
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");
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");
90 int parse_pid_string(const char *_pid_string
,
91 int all
, int **_pid_list
, int *nr_pids
)
93 const char *one_pid_str
;
95 int retval
= CMD_SUCCESS
;
98 char *pid_string
= NULL
;
100 if (all
&& _pid_string
) {
101 ERR("An empty PID string is expected with --all");
105 if (!all
&& !_pid_string
) {
106 ERR("Please specify --all with an empty PID string");
111 pid_list
= zmalloc(sizeof(*_pid_list
));
113 ERR("Out of memory");
117 /* Empty PID string means all PIDs */
123 pid_string
= strdup(_pid_string
);
125 ERR("Out of memory");
131 one_pid_str
= strtok_r(pid_string
, ",", &iter
);
132 while (one_pid_str
!= NULL
) {
135 v
= strtoul(one_pid_str
, NULL
, 10);
136 if ((v
== 0 && errno
== EINVAL
)
137 || (v
== ULONG_MAX
&& errno
== ERANGE
)) {
138 ERR("Error parsing PID %s", one_pid_str
);
142 if ((long) v
> INT_MAX
|| (int) v
< 0) {
143 ERR("Invalid PID value %ld", (long) v
);
150 one_pid_str
= strtok_r(NULL
, ",", &iter
);
154 /* Identity of delimiter has been lost in first pass. */
155 pid_string
= strdup(_pid_string
);
157 ERR("Out of memory");
163 pid_list
= zmalloc(count
* sizeof(*pid_list
));
165 ERR("Out of memory");
172 one_pid_str
= strtok_r(pid_string
, ",", &iter
);
173 while (one_pid_str
!= NULL
) {
176 v
= strtoul(one_pid_str
, NULL
, 10);
177 pid_list
[count
++] = (int) v
;
180 one_pid_str
= strtok_r(NULL
, ",", &iter
);
185 *_pid_list
= pid_list
;
186 goto end
; /* SUCCESS */
197 int track_untrack_pid(enum cmd_type cmd_type
, const char *cmd_str
,
198 const char *session_name
, const char *pid_string
,
199 int all
, struct mi_writer
*writer
)
201 int ret
, retval
= CMD_SUCCESS
, i
;
202 int *pid_list
= NULL
;
204 struct lttng_domain dom
;
205 struct lttng_handle
*handle
= NULL
;
206 int (*lib_func
)(struct lttng_handle
*handle
, int pid
);
210 lib_func
= lttng_track_pid
;
213 lib_func
= lttng_untrack_pid
;
216 ERR("Unknown command");
221 memset(&dom
, 0, sizeof(dom
));
223 dom
.type
= LTTNG_DOMAIN_KERNEL
;
224 } else if (opt_userspace
) {
225 dom
.type
= LTTNG_DOMAIN_UST
;
227 print_missing_domain();
232 ret
= parse_pid_string(pid_string
, all
, &pid_list
, &nr_pids
);
233 if (ret
!= CMD_SUCCESS
) {
234 ERR("Error parsing PID string");
235 usage(stderr
, cmd_str
);
240 handle
= lttng_create_handle(session_name
, &dom
);
241 if (handle
== NULL
) {
247 /* Open pids element */
248 ret
= mi_lttng_writer_open_element(writer
, config_element_pids
);
256 for (i
= 0; i
< nr_pids
; i
++) {
257 DBG("%s PID %d", cmd_str
, pid_list
[i
]);
258 ret
= lib_func(handle
, pid_list
[i
]);
266 /* Close pids element */
267 ret
= mi_lttng_writer_close_element(writer
);
277 lttng_destroy_handle(handle
);
284 const char *get_mi_element_command(enum cmd_type cmd_type
)
288 return mi_lttng_element_command_track
;
290 return mi_lttng_element_command_untrack
;
297 * Add/remove tracker to/from session.
300 int cmd_track_untrack(enum cmd_type cmd_type
, const char *cmd_str
,
301 int argc
, const char **argv
)
303 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
305 static poptContext pc
;
306 char *session_name
= NULL
;
307 struct mi_writer
*writer
= NULL
;
310 usage(stderr
, cmd_str
);
315 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
316 poptReadDefaultConfig(pc
, 0);
318 while ((opt
= poptGetNextOpt(pc
)) != -1) {
321 usage(stdout
, cmd_str
);
323 case OPT_LIST_OPTIONS
:
324 list_cmd_options(stdout
, long_options
);
331 usage(stderr
, cmd_str
);
337 if (!(opt_userspace
^ opt_kernel
)) {
338 ERR("Exactly one of -u or -k needs to be specified.");
339 usage(stderr
, cmd_str
);
344 if (!opt_session_name
) {
345 session_name
= get_session_name();
346 if (session_name
== NULL
) {
351 session_name
= opt_session_name
;
354 /* Currently only PID tracker is supported */
356 ERR("Please specify at least one tracker with its expected arguments");
357 usage(stderr
, cmd_str
);
364 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
372 /* Open command element */
373 ret
= mi_lttng_writer_command_open(writer
,
374 get_mi_element_command(cmd_type
));
380 /* Open output element */
381 ret
= mi_lttng_writer_open_element(writer
,
382 mi_lttng_element_command_output
);
389 command_ret
= track_untrack_pid(cmd_type
,
390 cmd_str
, session_name
, opt_pid_string
,
398 /* Close output element */
399 ret
= mi_lttng_writer_close_element(writer
);
406 ret
= mi_lttng_writer_write_element_bool(writer
,
407 mi_lttng_element_command_success
, success
);
413 /* Command element close */
414 ret
= mi_lttng_writer_command_close(writer
);
422 if (!opt_session_name
) {
427 if (writer
&& mi_lttng_writer_destroy(writer
)) {
428 /* Preserve original error code */
429 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
432 /* Overwrite ret if an error occurred during track() */
433 ret
= command_ret
? command_ret
: ret
;
439 int cmd_track(int argc
, const char **argv
)
441 return cmd_track_untrack(CMD_TRACK
, "track", argc
, argv
);
444 int cmd_untrack(int argc
, const char **argv
)
446 return cmd_track_untrack(CMD_UNTRACK
, "untrack", argc
, argv
);