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>
28 #include "trace-kernel.h"
31 * Find the channel name for the given kernel session.
33 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
34 char *name
, struct ltt_kernel_session
*session
)
36 struct ltt_kernel_channel
*chan
;
38 if (session
== NULL
) {
39 ERR("Undefine session");
43 DBG("Trying to find channel %s", name
);
45 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
46 if (strcmp(name
, chan
->channel
->name
) == 0) {
47 DBG("Found channel by name %s", name
);
57 * Find the event name for the given channel.
59 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
60 char *name
, struct ltt_kernel_channel
*channel
)
62 struct ltt_kernel_event
*ev
;
64 if (channel
== NULL
) {
65 ERR("Undefine channel");
69 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
70 if (strcmp(name
, ev
->event
->name
) == 0) {
71 DBG("Found event by name %s for channel %s", name
,
72 channel
->channel
->name
);
82 * Allocate and initialize a kernel session data structure.
84 * Return pointer to structure or NULL.
86 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
89 struct ltt_kernel_session
*lks
;
91 /* Allocate a new ltt kernel session */
92 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
94 PERROR("create kernel session zmalloc");
98 /* Init data structure */
100 lks
->metadata_stream_fd
= -1;
101 lks
->channel_count
= 0;
102 lks
->stream_count_global
= 0;
103 lks
->metadata
= NULL
;
104 lks
->consumer_fd
= -1;
105 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
107 /* Create default consumer output object */
108 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
109 if (lks
->consumer
== NULL
) {
114 * The tmp_consumer stays NULL until a set_consumer_uri command is
115 * executed. At this point, the consumer should be nullify until an
116 * enable_consumer command. This assignment is symbolic since we've zmalloc
119 lks
->tmp_consumer
= NULL
;
121 /* Use the default consumer output which is the tracing session path. */
122 ret
= snprintf(lks
->consumer
->dst
.trace_path
, PATH_MAX
, "%s/kernel", path
);
124 PERROR("snprintf consumer trace path");
128 /* Set session path */
129 ret
= asprintf(&lks
->trace_path
, "%s/kernel", path
);
131 PERROR("asprintf kernel traces path");
142 * Allocate and initialize a kernel channel data structure.
144 * Return pointer to structure or NULL.
146 struct ltt_kernel_channel
*trace_kernel_create_channel(
147 struct lttng_channel
*chan
, char *path
)
149 struct ltt_kernel_channel
*lkc
;
151 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
153 PERROR("ltt_kernel_channel zmalloc");
157 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
158 if (lkc
->channel
== NULL
) {
159 PERROR("lttng_channel zmalloc");
162 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
165 lkc
->stream_count
= 0;
166 lkc
->event_count
= 0;
169 /* Init linked list */
170 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
171 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
180 * Allocate and initialize a kernel event. Set name and event type.
182 * Return pointer to structure or NULL.
184 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
186 struct ltt_kernel_event
*lke
;
187 struct lttng_kernel_event
*attr
;
189 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
190 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
191 if (lke
== NULL
|| attr
== NULL
) {
192 PERROR("kernel event zmalloc");
197 case LTTNG_EVENT_PROBE
:
198 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
199 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
200 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
201 strncpy(attr
->u
.kprobe
.symbol_name
,
202 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
203 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
205 case LTTNG_EVENT_FUNCTION
:
206 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
207 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
208 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
209 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
210 strncpy(attr
->u
.kretprobe
.symbol_name
,
211 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
212 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
214 case LTTNG_EVENT_FUNCTION_ENTRY
:
215 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
216 strncpy(attr
->u
.ftrace
.symbol_name
,
217 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
218 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
220 case LTTNG_EVENT_TRACEPOINT
:
221 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
223 case LTTNG_EVENT_SYSCALL
:
224 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
226 case LTTNG_EVENT_ALL
:
227 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
230 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
234 /* Copy event name */
235 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
236 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
238 /* Setting up a kernel event */
253 * Allocate and initialize a kernel metadata.
255 * Return pointer to structure or NULL.
257 struct ltt_kernel_metadata
*trace_kernel_create_metadata(char *path
)
259 struct ltt_kernel_metadata
*lkm
;
260 struct lttng_channel
*chan
;
262 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
263 chan
= zmalloc(sizeof(struct lttng_channel
));
264 if (lkm
== NULL
|| chan
== NULL
) {
265 PERROR("kernel metadata zmalloc");
269 /* Set default attributes */
270 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
271 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
272 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
273 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
274 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
275 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
290 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
293 * Return pointer to structure or NULL.
295 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
299 struct ltt_kernel_stream
*lks
;
301 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
303 PERROR("kernel stream zmalloc");
308 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%d", name
, count
);
310 PERROR("snprintf stream name");
313 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
326 * Cleanup kernel stream structure.
328 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
332 DBG("[trace] Closing stream fd %d", stream
->fd
);
333 /* Close kernel fd */
334 if (stream
->fd
>= 0) {
335 ret
= close(stream
->fd
);
340 /* Remove from stream list */
341 cds_list_del(&stream
->list
);
347 * Cleanup kernel event structure.
349 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
353 if (event
->fd
>= 0) {
354 DBG("[trace] Closing event fd %d", event
->fd
);
355 /* Close kernel fd */
356 ret
= close(event
->fd
);
361 DBG("[trace] Tearing down event (no associated fd)");
364 /* Remove from event list */
365 cds_list_del(&event
->list
);
373 * Cleanup kernel channel structure.
375 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
377 struct ltt_kernel_stream
*stream
, *stmp
;
378 struct ltt_kernel_event
*event
, *etmp
;
381 DBG("[trace] Closing channel fd %d", channel
->fd
);
382 /* Close kernel fd */
383 if (channel
->fd
>= 0) {
384 ret
= close(channel
->fd
);
390 /* For each stream in the channel list */
391 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
392 trace_kernel_destroy_stream(stream
);
395 /* For each event in the channel list */
396 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
397 trace_kernel_destroy_event(event
);
400 /* Remove from channel list */
401 cds_list_del(&channel
->list
);
403 free(channel
->channel
);
409 * Cleanup kernel metadata structure.
411 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
415 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
416 /* Close kernel fd */
417 if (metadata
->fd
>= 0) {
418 ret
= close(metadata
->fd
);
424 free(metadata
->conf
);
429 * Cleanup kernel session structure
431 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
433 struct ltt_kernel_channel
*channel
, *ctmp
;
436 DBG("[trace] Closing session fd %d", session
->fd
);
437 /* Close kernel fds */
438 if (session
->fd
>= 0) {
439 ret
= close(session
->fd
);
445 if (session
->metadata_stream_fd
>= 0) {
446 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
447 ret
= close(session
->metadata_stream_fd
);
453 if (session
->metadata
!= NULL
) {
454 trace_kernel_destroy_metadata(session
->metadata
);
457 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
458 trace_kernel_destroy_channel(channel
);
461 /* Wipe consumer output object */
462 consumer_destroy_output(session
->consumer
);
463 consumer_destroy_output(session
->tmp_consumer
);
465 free(session
->trace_path
);