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.
27 #include "trace-kernel.h"
30 * Find the channel name for the given kernel session.
32 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
33 char *name
, struct ltt_kernel_session
*session
)
35 struct ltt_kernel_channel
*chan
;
37 if (session
== NULL
) {
38 ERR("Undefine session");
42 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
43 if (strcmp(name
, chan
->channel
->name
) == 0) {
44 DBG("Found channel by name %s", name
);
54 * Find the event name for the given channel.
56 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
57 char *name
, struct ltt_kernel_channel
*channel
)
59 struct ltt_kernel_event
*ev
;
61 if (channel
== NULL
) {
62 ERR("Undefine channel");
66 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
67 if (strcmp(name
, ev
->event
->name
) == 0) {
68 DBG("Found event by name %s for channel %s", name
,
69 channel
->channel
->name
);
79 * Allocate and initialize a kernel session data structure.
81 * Return pointer to structure or NULL.
83 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
86 struct ltt_kernel_session
*lks
;
88 /* Allocate a new ltt kernel session */
89 lks
= malloc(sizeof(struct ltt_kernel_session
));
91 perror("create kernel session malloc");
95 /* Init data structure */
97 lks
->metadata_stream_fd
= 0;
98 lks
->channel_count
= 0;
99 lks
->stream_count_global
= 0;
100 lks
->metadata
= NULL
;
101 lks
->consumer_fd
= 0;
102 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
104 /* Set session path */
105 ret
= asprintf(&lks
->trace_path
, "%s/kernel", path
);
107 perror("asprintf kernel traces path");
118 * Allocate and initialize a kernel channel data structure.
120 * Return pointer to structure or NULL.
122 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
, char *path
)
125 struct ltt_kernel_channel
*lkc
;
127 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
129 perror("ltt_kernel_channel malloc");
133 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
134 if (lkc
->channel
== NULL
) {
135 perror("lttng_channel malloc");
138 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
141 lkc
->stream_count
= 0;
142 lkc
->event_count
= 0;
145 /* Init linked list */
146 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
147 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
148 /* Set default trace output path */
149 ret
= asprintf(&lkc
->pathname
, "%s", path
);
151 perror("asprintf kernel create channel");
162 * Allocate and initialize a kernel event. Set name and event type.
164 * Return pointer to structure or NULL.
166 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
168 struct ltt_kernel_event
*lke
;
169 struct lttng_kernel_event
*attr
;
171 lke
= malloc(sizeof(struct ltt_kernel_event
));
172 attr
= malloc(sizeof(struct lttng_kernel_event
));
173 if (lke
== NULL
|| attr
== NULL
) {
174 perror("kernel event malloc");
179 case LTTNG_EVENT_PROBE
:
180 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
181 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
182 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
183 strncpy(attr
->u
.kprobe
.symbol_name
,
184 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
185 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
187 case LTTNG_EVENT_FUNCTION
:
188 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
189 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
190 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
191 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
192 strncpy(attr
->u
.kretprobe
.symbol_name
,
193 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
194 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
196 case LTTNG_EVENT_FUNCTION_ENTRY
:
197 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
198 strncpy(attr
->u
.ftrace
.symbol_name
,
199 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
200 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
202 case LTTNG_EVENT_TRACEPOINT
:
203 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
206 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
210 /* Copy event name */
211 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
212 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
214 /* Setting up a kernel event */
227 * Allocate and initialize a kernel metadata.
229 * Return pointer to structure or NULL.
231 struct ltt_kernel_metadata
*trace_kernel_create_metadata(char *path
)
234 struct ltt_kernel_metadata
*lkm
;
235 struct lttng_channel
*chan
;
237 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
238 chan
= malloc(sizeof(struct lttng_channel
));
239 if (lkm
== NULL
|| chan
== NULL
) {
240 perror("kernel metadata malloc");
244 /* Set default attributes */
245 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
246 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
247 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
248 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
249 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
250 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
255 /* Set default metadata path */
256 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
258 perror("asprintf kernel metadata");
269 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
272 * Return pointer to structure or NULL.
274 struct ltt_kernel_stream
*trace_kernel_create_stream(void)
276 struct ltt_kernel_stream
*lks
;
278 lks
= malloc(sizeof(struct ltt_kernel_stream
));
280 perror("kernel stream malloc");
286 lks
->pathname
= NULL
;
296 * Cleanup kernel stream structure.
298 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
300 DBG("[trace] Closing stream fd %d", stream
->fd
);
301 /* Close kernel fd */
303 /* Remove from stream list */
304 cds_list_del(&stream
->list
);
306 free(stream
->pathname
);
311 * Cleanup kernel event structure.
313 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
315 DBG("[trace] Closing event fd %d", event
->fd
);
316 /* Close kernel fd */
319 /* Remove from event list */
320 cds_list_del(&event
->list
);
328 * Cleanup kernel channel structure.
330 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
332 struct ltt_kernel_stream
*stream
, *stmp
;
333 struct ltt_kernel_event
*event
, *etmp
;
335 DBG("[trace] Closing channel fd %d", channel
->fd
);
336 /* Close kernel fd */
339 /* For each stream in the channel list */
340 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
341 trace_kernel_destroy_stream(stream
);
344 /* For each event in the channel list */
345 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
346 trace_kernel_destroy_event(event
);
349 /* Remove from channel list */
350 cds_list_del(&channel
->list
);
352 free(channel
->pathname
);
353 free(channel
->channel
);
359 * Cleanup kernel metadata structure.
361 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
363 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
364 /* Close kernel fd */
367 free(metadata
->conf
);
368 free(metadata
->pathname
);
373 * Cleanup kernel session structure
375 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
377 struct ltt_kernel_channel
*channel
, *ctmp
;
379 DBG("[trace] Closing session fd %d", session
->fd
);
380 /* Close kernel fds */
383 if (session
->metadata_stream_fd
!= 0) {
384 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
385 close(session
->metadata_stream_fd
);
388 if (session
->metadata
!= NULL
) {
389 trace_kernel_destroy_metadata(session
->metadata
);
392 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
393 trace_kernel_destroy_channel(channel
);
396 free(session
->trace_path
);