2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/common.h>
25 #include <common/defaults.h>
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 DBG("Trying to find channel %s", name
);
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 * Find the event name for the given channel.
58 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
59 char *name
, struct ltt_kernel_channel
*channel
)
61 struct ltt_kernel_event
*ev
;
63 if (channel
== NULL
) {
64 ERR("Undefine channel");
68 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
69 if (strcmp(name
, ev
->event
->name
) == 0) {
70 DBG("Found event by name %s for channel %s", name
,
71 channel
->channel
->name
);
81 * Allocate and initialize a kernel session data structure.
83 * Return pointer to structure or NULL.
85 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
88 struct ltt_kernel_session
*lks
;
90 /* Allocate a new ltt kernel session */
91 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
93 PERROR("create kernel session zmalloc");
97 /* Init data structure */
99 lks
->metadata_stream_fd
= -1;
100 lks
->channel_count
= 0;
101 lks
->stream_count_global
= 0;
102 lks
->metadata
= NULL
;
103 lks
->consumer_fd
= -1;
104 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
106 /* Set session path */
107 ret
= asprintf(&lks
->trace_path
, "%s/kernel", path
);
109 PERROR("asprintf kernel traces path");
120 * Allocate and initialize a kernel channel data structure.
122 * Return pointer to structure or NULL.
124 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
, char *path
)
127 struct ltt_kernel_channel
*lkc
;
129 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
131 PERROR("ltt_kernel_channel zmalloc");
135 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
136 if (lkc
->channel
== NULL
) {
137 PERROR("lttng_channel zmalloc");
140 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
143 lkc
->stream_count
= 0;
144 lkc
->event_count
= 0;
147 /* Init linked list */
148 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
149 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
150 /* Set default trace output path */
151 ret
= asprintf(&lkc
->pathname
, "%s", path
);
153 PERROR("asprintf kernel create channel");
164 * Allocate and initialize a kernel event. Set name and event type.
166 * Return pointer to structure or NULL.
168 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
170 struct ltt_kernel_event
*lke
;
171 struct lttng_kernel_event
*attr
;
173 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
174 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
175 if (lke
== NULL
|| attr
== NULL
) {
176 PERROR("kernel event zmalloc");
181 case LTTNG_EVENT_PROBE
:
182 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
183 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
184 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
185 strncpy(attr
->u
.kprobe
.symbol_name
,
186 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
187 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
189 case LTTNG_EVENT_FUNCTION
:
190 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
191 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
192 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
193 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
194 strncpy(attr
->u
.kretprobe
.symbol_name
,
195 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
196 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
198 case LTTNG_EVENT_FUNCTION_ENTRY
:
199 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
200 strncpy(attr
->u
.ftrace
.symbol_name
,
201 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
202 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
204 case LTTNG_EVENT_TRACEPOINT
:
205 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
207 case LTTNG_EVENT_SYSCALL
:
208 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
210 case LTTNG_EVENT_ALL
:
211 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
214 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
218 /* Copy event name */
219 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
220 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
222 /* Setting up a kernel event */
235 * Allocate and initialize a kernel metadata.
237 * Return pointer to structure or NULL.
239 struct ltt_kernel_metadata
*trace_kernel_create_metadata(char *path
)
242 struct ltt_kernel_metadata
*lkm
;
243 struct lttng_channel
*chan
;
245 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
246 chan
= zmalloc(sizeof(struct lttng_channel
));
247 if (lkm
== NULL
|| chan
== NULL
) {
248 PERROR("kernel metadata zmalloc");
252 /* Set default attributes */
253 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
254 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
255 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
256 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
257 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
258 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
263 /* Set default metadata path */
264 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
266 PERROR("asprintf kernel metadata");
277 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
280 * Return pointer to structure or NULL.
282 struct ltt_kernel_stream
*trace_kernel_create_stream(void)
284 struct ltt_kernel_stream
*lks
;
286 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
288 PERROR("kernel stream zmalloc");
294 lks
->pathname
= NULL
;
304 * Cleanup kernel stream structure.
306 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
310 DBG("[trace] Closing stream fd %d", stream
->fd
);
311 /* Close kernel fd */
312 if (stream
->fd
>= 0) {
313 ret
= close(stream
->fd
);
318 /* Remove from stream list */
319 cds_list_del(&stream
->list
);
321 free(stream
->pathname
);
326 * Cleanup kernel event structure.
328 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
332 if (event
->fd
>= 0) {
333 DBG("[trace] Closing event fd %d", event
->fd
);
334 /* Close kernel fd */
335 ret
= close(event
->fd
);
340 DBG("[trace] Tearing down event (no associated fd)");
343 /* Remove from event list */
344 cds_list_del(&event
->list
);
352 * Cleanup kernel channel structure.
354 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
356 struct ltt_kernel_stream
*stream
, *stmp
;
357 struct ltt_kernel_event
*event
, *etmp
;
360 DBG("[trace] Closing channel fd %d", channel
->fd
);
361 /* Close kernel fd */
362 if (channel
->fd
>= 0) {
363 ret
= close(channel
->fd
);
369 /* For each stream in the channel list */
370 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
371 trace_kernel_destroy_stream(stream
);
374 /* For each event in the channel list */
375 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
376 trace_kernel_destroy_event(event
);
379 /* Remove from channel list */
380 cds_list_del(&channel
->list
);
382 free(channel
->pathname
);
383 free(channel
->channel
);
389 * Cleanup kernel metadata structure.
391 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
395 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
396 /* Close kernel fd */
397 if (metadata
->fd
>= 0) {
398 ret
= close(metadata
->fd
);
404 free(metadata
->conf
);
405 free(metadata
->pathname
);
410 * Cleanup kernel session structure
412 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
414 struct ltt_kernel_channel
*channel
, *ctmp
;
417 DBG("[trace] Closing session fd %d", session
->fd
);
418 /* Close kernel fds */
419 if (session
->fd
>= 0) {
420 ret
= close(session
->fd
);
426 if (session
->metadata_stream_fd
>= 0) {
427 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
428 ret
= close(session
->metadata_stream_fd
);
434 if (session
->metadata
!= NULL
) {
435 trace_kernel_destroy_metadata(session
->metadata
);
438 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
439 trace_kernel_destroy_channel(channel
);
442 free(session
->trace_path
);