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; only version 2
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>
30 * get_kernel_channel_by_name
32 * Find the channel name for the given kernel session.
34 struct ltt_kernel_channel
*get_kernel_channel_by_name(
35 char *name
, struct ltt_kernel_session
*session
)
37 struct ltt_kernel_channel
*chan
;
39 if (session
== NULL
) {
40 ERR("Undefine session");
44 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
45 if (strcmp(name
, chan
->channel
->name
) == 0) {
46 DBG("Found channel by name %s", name
);
56 * get_kernel_event_by_name
58 * Find the event name for the given channel.
60 struct ltt_kernel_event
*get_kernel_event_by_name(
61 char *name
, struct ltt_kernel_channel
*channel
)
63 struct ltt_kernel_event
*ev
;
65 if (channel
== NULL
) {
66 ERR("Undefine channel");
70 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
71 if (strcmp(name
, ev
->event
->name
) == 0) {
72 DBG("Found event by name %s for channel %s", name
,
73 channel
->channel
->name
);
83 * trace_create_kernel_session
85 * Allocate and initialize a kernel session data structure.
87 * Return pointer to structure or NULL.
89 struct ltt_kernel_session
*trace_create_kernel_session(void)
91 struct ltt_kernel_session
*lks
;
93 /* Allocate a new ltt kernel session */
94 lks
= malloc(sizeof(struct ltt_kernel_session
));
96 perror("create kernel session malloc");
100 /* Init data structure */
102 lks
->metadata_stream_fd
= 0;
103 lks
->channel_count
= 0;
104 lks
->stream_count_global
= 0;
105 lks
->metadata
= NULL
;
106 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
115 * trace_create_kernel_channel
117 * Allocate and initialize a kernel channel data structure.
119 * Return pointer to structure or NULL.
121 struct ltt_kernel_channel
*trace_create_kernel_channel(struct lttng_channel
*chan
, char *path
)
124 struct ltt_kernel_channel
*lkc
;
126 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
128 perror("ltt_kernel_channel malloc");
132 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
133 if (lkc
->channel
== NULL
) {
134 perror("lttng_channel malloc");
137 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
140 lkc
->stream_count
= 0;
141 lkc
->event_count
= 0;
144 /* Init linked list */
145 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
146 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
147 /* Set default trace output path */
148 ret
= asprintf(&lkc
->pathname
, "%s", path
);
150 perror("asprintf kernel create channel");
161 * trace_create_kernel_event
163 * Allocate and initialize a kernel event. Set name and event type.
165 * Return pointer to structure or NULL.
167 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
169 struct ltt_kernel_event
*lke
;
170 struct lttng_kernel_event
*attr
;
172 lke
= malloc(sizeof(struct ltt_kernel_event
));
173 attr
= malloc(sizeof(struct lttng_kernel_event
));
174 if (lke
== NULL
|| attr
== NULL
) {
175 perror("kernel event malloc");
180 case LTTNG_EVENT_PROBE
:
181 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
182 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
183 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
184 strncpy(attr
->u
.kprobe
.symbol_name
,
185 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
186 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
188 case LTTNG_EVENT_FUNCTION
:
189 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
190 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
191 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
192 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
193 strncpy(attr
->u
.kretprobe
.symbol_name
,
194 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
195 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
197 case LTTNG_EVENT_FUNCTION_ENTRY
:
198 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
199 strncpy(attr
->u
.ftrace
.symbol_name
,
200 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
201 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
203 case LTTNG_EVENT_TRACEPOINT
:
204 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
207 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
211 /* Copy event name */
212 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
213 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
215 /* Setting up a kernel event */
228 * trace_create_kernel_metadata
230 * Allocate and initialize a kernel metadata.
232 * Return pointer to structure or NULL.
234 struct ltt_kernel_metadata
*trace_create_kernel_metadata(char *path
)
237 struct ltt_kernel_metadata
*lkm
;
238 struct lttng_channel
*chan
;
240 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
241 chan
= malloc(sizeof(struct lttng_channel
));
242 if (lkm
== NULL
|| chan
== NULL
) {
243 perror("kernel metadata malloc");
247 /* Set default attributes */
248 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
249 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
250 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
251 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
252 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
253 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
258 /* Set default metadata path */
259 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
261 perror("asprintf kernel metadata");
272 * trace_create_kernel_stream
274 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
277 * Return pointer to structure or NULL.
279 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
281 struct ltt_kernel_stream
*lks
;
283 lks
= malloc(sizeof(struct ltt_kernel_stream
));
285 perror("kernel stream malloc");
291 lks
->pathname
= NULL
;
300 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
302 DBG("[trace] Closing stream fd %d", stream
->fd
);
303 /* Close kernel fd */
305 free(stream
->pathname
);
307 /* Remove from stream list */
308 cds_list_del(&stream
->list
);
312 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
314 DBG("[trace] Closing event fd %d", event
->fd
);
315 /* Close kernel fd */
317 /* Free attributes */
320 /* Remove from event list */
321 cds_list_del(&event
->list
);
325 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
327 struct ltt_kernel_stream
*stream
, *stmp
;
328 struct ltt_kernel_event
*event
, *etmp
;
330 DBG("[trace] Closing channel fd %d", channel
->fd
);
331 /* Close kernel fd */
333 free(channel
->pathname
);
334 /* Free attributes structure */
335 free(channel
->channel
);
337 /* For each stream in the channel list */
338 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
339 trace_destroy_kernel_stream(stream
);
342 /* For each event in the channel list */
343 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
344 trace_destroy_kernel_event(event
);
347 /* Remove from channel list */
348 cds_list_del(&channel
->list
);
352 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
354 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
355 /* Close kernel fd */
357 /* Free attributes */
358 free(metadata
->conf
);
363 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
365 struct ltt_kernel_channel
*channel
, *ctmp
;
367 DBG("[trace] Closing session fd %d", session
->fd
);
368 /* Close kernel fds */
370 if (session
->metadata_stream_fd
!= 0) {
371 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
372 close(session
->metadata_stream_fd
);
375 if (session
->metadata
!= NULL
) {
376 trace_destroy_kernel_metadata(session
->metadata
);
379 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
380 trace_destroy_kernel_channel(channel
);