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.
24 #include <urcu/list.h>
27 #include "ltt-sessiond.h"
31 * trace_create_kernel_session
33 * Allocate and initialize a kernel session data structure.
35 * Return pointer to structure or NULL.
37 struct ltt_kernel_session
*trace_create_kernel_session(void)
39 struct ltt_kernel_session
*lks
;
41 /* Allocate a new ltt kernel session */
42 lks
= malloc(sizeof(struct ltt_kernel_session
));
44 perror("create kernel session malloc");
48 /* Init data structure */
50 lks
->metadata_stream_fd
= 0;
51 lks
->channel_count
= 0;
52 lks
->stream_count_global
= 0;
54 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
63 * trace_create_kernel_channel
65 * Allocate and initialize a kernel channel data structure.
67 * Return pointer to structure or NULL.
69 struct ltt_kernel_channel
*trace_create_kernel_channel(struct lttng_channel
*chan
)
72 struct ltt_kernel_channel
*lkc
;
74 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
76 perror("ltt_kernel_channel malloc");
80 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
81 if (lkc
->channel
== NULL
) {
82 perror("lttng_channel malloc");
85 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
88 lkc
->stream_count
= 0;
89 /* Init linked list */
90 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
91 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
92 /* Set default trace output path */
93 ret
= asprintf(&lkc
->pathname
, "%s", DEFAULT_TRACE_OUTPUT
);
95 perror("asprintf kernel create channel");
106 * trace_create_kernel_event
108 * Allocate and initialize a kernel event. Set name and event type.
110 * Return pointer to structure or NULL.
112 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
114 struct ltt_kernel_event
*lke
;
115 struct lttng_kernel_event
*attr
;
117 lke
= malloc(sizeof(struct ltt_kernel_event
));
118 attr
= malloc(sizeof(struct lttng_kernel_event
));
119 if (lke
== NULL
|| attr
== NULL
) {
120 perror("kernel event malloc");
125 case LTTNG_EVENT_KPROBES
:
126 attr
->instrumentation
= LTTNG_KERNEL_KPROBES
;
127 attr
->u
.kprobe
.addr
= ev
->attr
.kprobe
.addr
;
128 attr
->u
.kprobe
.offset
= ev
->attr
.kprobe
.offset
;
129 strncpy(attr
->u
.kprobe
.symbol_name
,
130 ev
->attr
.kprobe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
132 case LTTNG_EVENT_FUNCTION
:
133 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
134 strncpy(attr
->u
.ftrace
.symbol_name
,
135 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
137 case LTTNG_EVENT_TRACEPOINTS
:
138 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINTS
;
141 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
145 /* Copy event name */
146 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
148 /* Setting up a kernel event */
159 * trace_create_kernel_metadata
161 * Allocate and initialize a kernel metadata.
163 * Return pointer to structure or NULL.
165 struct ltt_kernel_metadata
*trace_create_kernel_metadata(void)
168 struct ltt_kernel_metadata
*lkm
;
169 struct lttng_channel
*chan
;
171 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
172 chan
= malloc(sizeof(struct lttng_channel
));
173 if (lkm
== NULL
|| chan
== NULL
) {
174 perror("kernel metadata malloc");
178 /* Set default attributes */
179 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
180 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
181 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
182 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
183 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
188 /* Set default metadata path */
189 ret
= asprintf(&lkm
->pathname
, "%s/metadata", DEFAULT_TRACE_OUTPUT
);
191 perror("asprintf kernel metadata");
202 * trace_create_kernel_stream
204 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
207 * Return pointer to structure or NULL.
209 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
211 struct ltt_kernel_stream
*lks
;
213 lks
= malloc(sizeof(struct ltt_kernel_stream
));
215 perror("kernel stream malloc");
221 lks
->pathname
= NULL
;
230 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
232 DBG("[trace] Closing stream fd %d", stream
->fd
);
233 /* Close kernel fd */
235 free(stream
->pathname
);
237 /* Remove from stream list */
238 cds_list_del(&stream
->list
);
242 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
244 DBG("[trace] Closing event fd %d", event
->fd
);
245 /* Close kernel fd */
247 /* Free attributes */
250 /* Remove from event list */
251 cds_list_del(&event
->list
);
255 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
257 struct ltt_kernel_stream
*stream
;
258 struct ltt_kernel_event
*event
;
260 DBG("[trace] Closing channel fd %d", channel
->fd
);
261 /* Close kernel fd */
263 free(channel
->pathname
);
264 /* Free attributes structure */
265 free(channel
->channel
);
267 /* For each stream in the channel list */
268 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
269 trace_destroy_kernel_stream(stream
);
272 /* For each event in the channel list */
273 cds_list_for_each_entry(event
, &channel
->events_list
.head
, list
) {
274 trace_destroy_kernel_event(event
);
277 /* Remove from channel list */
278 cds_list_del(&channel
->list
);
282 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
284 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
285 /* Close kernel fd */
287 /* Free attributes */
288 free(metadata
->conf
);
293 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
295 struct ltt_kernel_channel
*channel
;
297 DBG("[trace] Closing session fd %d", session
->fd
);
298 /* Close kernel fds */
300 if (session
->metadata_stream_fd
!= 0) {
301 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
302 close(session
->metadata_stream_fd
);
305 trace_destroy_kernel_metadata(session
->metadata
);
307 cds_list_for_each_entry(channel
, &session
->channel_list
.head
, list
) {
308 trace_destroy_kernel_channel(channel
);