2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
32 static char *opt_channel_name
;
33 static char *opt_kernel
;
34 static char *opt_cmd_name
;
35 static char *opt_session_name
;
36 static int opt_pid_all
;
37 static int opt_userspace
;
39 static struct lttng_channel chan
;
52 static struct poptOption long_options
[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
55 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
56 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
57 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_USERSPACE
, 0, 0},
58 {"all", 0, POPT_ARG_VAL
, &opt_pid_all
, 1, 0, 0},
59 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
60 {"discard", 0, POPT_ARG_NONE
, 0, OPT_DISCARD
, 0, 0},
61 {"overwrite", 0, POPT_ARG_NONE
, 0, OPT_OVERWRITE
, 0, 0},
62 {"subbuf_size", 0, POPT_ARG_DOUBLE
, 0, OPT_SUBBUF_SIZE
, 0, 0},
63 {"num_subbuf", 0, POPT_ARG_INT
, 0, OPT_NUM_SUBBUF
, 0, 0},
64 {"switch_timer", 0, POPT_ARG_INT
, 0, OPT_SWITCH_TIMER
, 0, 0},
65 {"read_timer", 0, POPT_ARG_INT
, 0, OPT_READ_TIMER
, 0, 0},
72 static void usage(FILE *ofp
)
74 fprintf(ofp
, "usage: lttng add-channel NAME [options] [channel_options]\n");
76 fprintf(ofp
, " -h, --help Show this help\n");
77 fprintf(ofp
, " -s, --session Apply on session name\n");
78 fprintf(ofp
, " -k, --kernel Apply on the kernel tracer\n");
79 fprintf(ofp
, " -u, --userspace [CMD] Apply on the user-space tracer\n");
80 fprintf(ofp
, " --all If -u, apply on all traceable apps\n");
81 fprintf(ofp
, " -p, --pid PID If -u, apply on a specific PID\n");
83 fprintf(ofp
, "Channel options:\n");
84 fprintf(ofp
, " --discard Discard event when buffers are full (default)\n");
85 fprintf(ofp
, " --overwrite Flight recorder mode\n");
86 fprintf(ofp
, " --subbuf_size Subbuffer size in bytes (default: 4096)\n");
87 fprintf(ofp
, " --num_subbuf Number of subbufers (default: 2)\n");
88 fprintf(ofp
, " --switch_timer Switch timer interval in usec (default: 0)\n");
89 fprintf(ofp
, " --read_timer Read timer interval in usec (default: 200)\n");
96 * Adding channel using the lttng API.
98 static int add_channel(void)
100 int ret
= CMD_SUCCESS
;
102 if (set_session_name(opt_session_name
) < 0) {
107 /* Copy channel name and normalize it */
108 strncpy(chan
.name
, opt_channel_name
, NAME_MAX
);
109 chan
.name
[NAME_MAX
- 1] = '\0';
111 /* Kernel tracer action */
113 /* Create kernel channel */
114 ret
= lttng_kernel_create_channel(&chan
);
118 } else if (opt_userspace
) { /* User-space tracer action */
120 * TODO: Waiting on lttng UST 2.0
123 } else if (opt_pid
!= 0) {
125 ret
= CMD_NOT_IMPLEMENTED
;
128 ERR("Please specify a tracer (kernel or user-space)");
132 MSG("Channel %s created", opt_channel_name
);
139 * init_channel_config
141 * Default value for channel configuration.
143 static void init_channel_config(void)
145 chan
.attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
146 chan
.attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
147 chan
.attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
148 chan
.attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
149 chan
.attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
150 chan
.attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
156 * Add channel to trace session
158 int cmd_add_channel(int argc
, const char **argv
)
161 static poptContext pc
;
163 init_channel_config();
165 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
166 poptReadDefaultConfig(pc
, 0);
168 while ((opt
= poptGetNextOpt(pc
)) != -1) {
176 opt_cmd_name
= poptGetOptArg(pc
);
179 chan
.attr
.overwrite
= 0;
180 DBG("Channel set to discard");
183 chan
.attr
.overwrite
= 1;
184 DBG("Channel set to overwrite");
186 case OPT_SUBBUF_SIZE
:
187 chan
.attr
.subbuf_size
= atol(poptGetOptArg(pc
));
188 DBG("Channel subbuf size set to %lu", chan
.attr
.subbuf_size
);
191 chan
.attr
.num_subbuf
= atoi(poptGetOptArg(pc
));
192 DBG("Channel subbuf num set to %lu", chan
.attr
.num_subbuf
);
194 case OPT_SWITCH_TIMER
:
195 chan
.attr
.switch_timer_interval
= atoi(poptGetOptArg(pc
));
196 DBG("Channel switch timer interval set to %d", chan
.attr
.switch_timer_interval
);
199 chan
.attr
.read_timer_interval
= atoi(poptGetOptArg(pc
));
200 DBG("Channel read timer interval set to %d", chan
.attr
.read_timer_interval
);
209 opt_channel_name
= (char*) poptGetArg(pc
);
210 if (opt_channel_name
== NULL
) {
211 ERR("Missing channel name.\n");