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"
32 * kernel_create_session
34 * Create a new kernel session using the command context session.
36 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
39 struct ltt_kernel_session
*lks
;
41 /* Allocate a new kernel session */
42 lks
= malloc(sizeof(struct ltt_kernel_session
));
44 perror("kernel session malloc");
49 ret
= kernctl_create_session(tracer_fd
);
54 /* Assigning session fd and to the command context */
56 lks
->channel_count
= 0;
57 lks
->stream_count_global
= 0;
58 session
->kernel_session
= lks
;
59 session
->kern_session_count
++;
60 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
62 DBG("Kernel session created (fd: %d)", lks
->fd
);
71 * kernel_create_channel
73 * Create a kernel channel within the kernel session.
75 int kernel_create_channel(struct ltt_kernel_session
*session
)
78 struct ltt_kernel_channel
*lkc
;
79 struct lttng_kernel_channel
*chan
;
81 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
82 chan
= malloc(sizeof(struct lttng_kernel_channel
));
84 if (lkc
== NULL
|| chan
== NULL
) {
85 perror("kernel channel malloc");
90 chan
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
91 chan
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
92 chan
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
93 chan
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
94 chan
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
96 ret
= kernctl_create_channel(session
->fd
, chan
);
98 perror("ioctl create channel");
102 /* Setup the channel */
105 lkc
->stream_count
= 0;
106 ret
= asprintf(&lkc
->pathname
, "%s", DEFAULT_TRACE_OUTPUT
);
108 perror("asprintf kernel create channel");
112 DBG("Channel path set to %s", lkc
->pathname
);
114 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
115 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
116 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
117 session
->channel_count
++;
119 DBG("Kernel channel created (fd: %d)", lkc
->fd
);
128 * kernel_enable_event
130 * Enable kernel event.
132 int kernel_enable_event(struct ltt_kernel_session
*session
, char *name
)
135 struct ltt_kernel_channel
*chan
;
136 struct ltt_kernel_event
*event
;
137 struct lttng_kernel_event
*lke
;
139 event
= malloc(sizeof(struct ltt_kernel_event
));
140 lke
= malloc(sizeof(struct lttng_kernel_event
));
142 if (event
== NULL
|| lke
== NULL
) {
143 perror("kernel enable event malloc");
148 /* Setting up a kernel event */
149 strncpy(lke
->name
, name
, LTTNG_SYM_NAME_LEN
);
150 lke
->instrumentation
= LTTNG_KERNEL_TRACEPOINTS
;
153 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
154 ret
= kernctl_create_event(chan
->fd
, lke
);
160 /* Add event to event list */
161 cds_list_add(&event
->list
, &chan
->events_list
.head
);
162 DBG("Event %s enabled (fd: %d)", name
, event
->fd
);
172 * kernel_open_metadata
174 * Open metadata stream.
176 int kernel_open_metadata(struct ltt_kernel_session
*session
)
179 struct ltt_kernel_metadata
*lkm
;
180 struct lttng_kernel_channel
*conf
;
182 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
183 conf
= malloc(sizeof(struct lttng_kernel_channel
));
185 if (lkm
== NULL
|| conf
== NULL
) {
186 perror("kernel open metadata malloc");
191 conf
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
192 conf
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
193 conf
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
194 conf
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
195 conf
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
197 ret
= kernctl_open_metadata(session
->fd
, conf
);
204 ret
= asprintf(&lkm
->pathname
, "%s/metadata", DEFAULT_TRACE_OUTPUT
);
206 perror("asprintf kernel metadata");
209 session
->metadata
= lkm
;
211 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
220 * kernel_start_session
222 * Start tracing session.
224 int kernel_start_session(struct ltt_kernel_session
*session
)
228 ret
= kernctl_start_session(session
->fd
);
233 DBG("Kernel session started");
242 * kernel_stop_session
244 * Stop tracing session.
246 int kernel_stop_session(struct ltt_kernel_session
*session
)
250 ret
= kernctl_stop_session(session
->fd
);
255 DBG("Kernel session stopped");
264 * kernel_create_channel_stream
266 * Create a stream for a channel.
268 * Return the number of created stream. Else, a negative value.
270 int kernel_create_channel_stream(struct ltt_kernel_channel
*channel
)
273 struct ltt_kernel_stream
*lks
;
275 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
276 lks
= malloc(sizeof(struct ltt_kernel_stream
));
278 perror("kernel create stream malloc");
284 ret
= asprintf(&lks
->pathname
, "%s/trace_%d",
285 channel
->pathname
, channel
->stream_count
);
287 perror("asprintf kernel create stream");
292 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
293 channel
->stream_count
++;
296 DBG("Kernel channel stream created (num: %d)", channel
->stream_count
);
298 return channel
->stream_count
;
305 * kernel_create_metadata_stream
307 * Create the metadata stream.
309 int kernel_create_metadata_stream(struct ltt_kernel_session
*session
)
313 ret
= kernctl_create_stream(session
->metadata
->fd
);
315 perror("kernel create metadata stream");
320 DBG("Kernel metadata stream created (fd: %d)", ret
);
321 session
->metadata_stream_fd
= ret
;