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.
27 #include "ltt-sessiond.h"
28 #include "libkernelctl.h"
29 #include "kernel-ctl.h"
33 * kernel_create_session
35 * Create a new kernel session using the command context session.
37 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
40 struct ltt_kernel_session
*lks
;
42 /* Allocate a new kernel session */
43 lks
= malloc(sizeof(struct ltt_kernel_session
));
45 perror("kernel session malloc");
50 ret
= kernctl_create_session(tracer_fd
);
55 /* Assigning session fd and to the command context */
57 lks
->channel_count
= 0;
58 lks
->stream_count_global
= 0;
59 session
->kernel_session
= lks
;
60 session
->kern_session_count
++;
61 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
63 DBG("Kernel session created (fd: %d)", lks
->fd
);
72 * kernel_create_channel
74 * Create a kernel channel within the kernel session.
76 int kernel_create_channel(struct ltt_kernel_session
*session
)
79 struct ltt_kernel_channel
*lkc
;
80 struct lttng_kernel_channel
*chan
;
82 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
83 chan
= malloc(sizeof(struct lttng_kernel_channel
));
85 if (lkc
== NULL
|| chan
== NULL
) {
86 perror("kernel channel malloc");
91 chan
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
92 chan
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
93 chan
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
94 chan
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
95 chan
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
97 ret
= kernctl_create_channel(session
->fd
, chan
);
99 perror("ioctl create channel");
103 /* Setup the channel */
106 lkc
->stream_count
= 0;
107 ret
= asprintf(&lkc
->pathname
, "%s", DEFAULT_TRACE_OUTPUT
);
109 perror("asprintf kernel create channel");
113 DBG("Channel path set to %s", lkc
->pathname
);
115 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
116 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
117 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
118 session
->channel_count
++;
120 DBG("Kernel channel created (fd: %d)", lkc
->fd
);
129 * kernel_enable_event
131 * Enable kernel event.
133 int kernel_enable_event(struct ltt_kernel_session
*session
, char *name
)
136 struct ltt_kernel_channel
*chan
;
137 struct ltt_kernel_event
*event
;
138 struct lttng_kernel_event
*lke
;
140 event
= malloc(sizeof(struct ltt_kernel_event
));
141 lke
= malloc(sizeof(struct lttng_kernel_event
));
143 if (event
== NULL
|| lke
== NULL
) {
144 perror("kernel enable event malloc");
149 /* Setting up a kernel event */
150 strncpy(lke
->name
, name
, LTTNG_SYM_NAME_LEN
);
151 lke
->instrumentation
= LTTNG_KERNEL_TRACEPOINTS
;
154 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
155 ret
= kernctl_create_event(chan
->fd
, lke
);
161 /* Add event to event list */
162 cds_list_add(&event
->list
, &chan
->events_list
.head
);
163 DBG("Event %s enabled (fd: %d)", name
, event
->fd
);
173 * kernel_open_metadata
175 * Open metadata stream.
177 int kernel_open_metadata(struct ltt_kernel_session
*session
)
180 struct ltt_kernel_metadata
*lkm
;
181 struct lttng_kernel_channel
*conf
;
183 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
184 conf
= malloc(sizeof(struct lttng_kernel_channel
));
186 if (lkm
== NULL
|| conf
== NULL
) {
187 perror("kernel open metadata malloc");
192 conf
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
193 conf
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
194 conf
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
195 conf
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
196 conf
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
198 ret
= kernctl_open_metadata(session
->fd
, conf
);
205 ret
= asprintf(&lkm
->pathname
, "%s/metadata", DEFAULT_TRACE_OUTPUT
);
207 perror("asprintf kernel metadata");
210 session
->metadata
= lkm
;
212 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
221 * kernel_start_session
223 * Start tracing session.
225 int kernel_start_session(struct ltt_kernel_session
*session
)
229 ret
= kernctl_start_session(session
->fd
);
234 DBG("Kernel session started");
243 * kernel_stop_session
245 * Stop tracing session.
247 int kernel_stop_session(struct ltt_kernel_session
*session
)
251 ret
= kernctl_stop_session(session
->fd
);
256 DBG("Kernel session stopped");
265 * kernel_create_channel_stream
267 * Create a stream for a channel.
269 * Return the number of created stream. Else, a negative value.
271 int kernel_create_channel_stream(struct ltt_kernel_channel
*channel
)
274 struct ltt_kernel_stream
*lks
;
276 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
277 lks
= malloc(sizeof(struct ltt_kernel_stream
));
279 perror("kernel create stream malloc");
285 ret
= asprintf(&lks
->pathname
, "%s/trace_%d",
286 channel
->pathname
, channel
->stream_count
);
288 perror("asprintf kernel create stream");
293 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
294 channel
->stream_count
++;
297 DBG("Kernel channel stream created (num: %d)", channel
->stream_count
);
299 return channel
->stream_count
;
306 * kernel_create_metadata_stream
308 * Create the metadata stream.
310 int kernel_create_metadata_stream(struct ltt_kernel_session
*session
)
314 ret
= kernctl_create_stream(session
->metadata
->fd
);
316 perror("kernel create metadata stream");
321 DBG("Kernel metadata stream created (fd: %d)", ret
);
322 session
->metadata_stream_fd
= ret
;