Commit | Line | Data |
---|---|---|
54012638 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
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. | |
54012638 DG |
7 | * |
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. | |
12 | * | |
d14d33bf AM |
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. | |
54012638 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
c363b55d | 22 | #include <unistd.h> |
54012638 | 23 | |
990570ed DG |
24 | #include <common/common.h> |
25 | #include <common/defaults.h> | |
1e307fab | 26 | |
62499ad6 | 27 | #include "trace-kernel.h" |
54012638 | 28 | |
19e70852 | 29 | /* |
050349bb | 30 | * Find the channel name for the given kernel session. |
19e70852 | 31 | */ |
62499ad6 | 32 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
19e70852 DG |
33 | char *name, struct ltt_kernel_session *session) |
34 | { | |
35 | struct ltt_kernel_channel *chan; | |
36 | ||
37 | if (session == NULL) { | |
38 | ERR("Undefine session"); | |
39 | goto error; | |
40 | } | |
41 | ||
54d01ffb DG |
42 | DBG("Trying to find channel %s", name); |
43 | ||
19e70852 DG |
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); | |
47 | return chan; | |
48 | } | |
49 | } | |
50 | ||
51 | error: | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | /* | |
050349bb | 56 | * Find the event name for the given channel. |
19e70852 | 57 | */ |
62499ad6 | 58 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
19e70852 DG |
59 | char *name, struct ltt_kernel_channel *channel) |
60 | { | |
61 | struct ltt_kernel_event *ev; | |
62 | ||
63 | if (channel == NULL) { | |
64 | ERR("Undefine channel"); | |
65 | goto error; | |
66 | } | |
67 | ||
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); | |
72 | return ev; | |
73 | } | |
74 | } | |
75 | ||
76 | error: | |
77 | return NULL; | |
78 | } | |
79 | ||
54012638 | 80 | /* |
050349bb | 81 | * Allocate and initialize a kernel session data structure. |
54012638 | 82 | * |
050349bb | 83 | * Return pointer to structure or NULL. |
54012638 | 84 | */ |
f9815039 | 85 | struct ltt_kernel_session *trace_kernel_create_session(char *path) |
54012638 | 86 | { |
f9815039 | 87 | int ret; |
54012638 DG |
88 | struct ltt_kernel_session *lks; |
89 | ||
90 | /* Allocate a new ltt kernel session */ | |
ba7f0ae5 | 91 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
54012638 | 92 | if (lks == NULL) { |
df0f840b | 93 | PERROR("create kernel session zmalloc"); |
54012638 DG |
94 | goto error; |
95 | } | |
96 | ||
97 | /* Init data structure */ | |
03550b58 MD |
98 | lks->fd = -1; |
99 | lks->metadata_stream_fd = -1; | |
54012638 DG |
100 | lks->channel_count = 0; |
101 | lks->stream_count_global = 0; | |
102 | lks->metadata = NULL; | |
03550b58 | 103 | lks->consumer_fd = -1; |
54012638 DG |
104 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
105 | ||
f9815039 DG |
106 | /* Set session path */ |
107 | ret = asprintf(&lks->trace_path, "%s/kernel", path); | |
108 | if (ret < 0) { | |
df0f840b | 109 | PERROR("asprintf kernel traces path"); |
f9815039 DG |
110 | goto error; |
111 | } | |
112 | ||
54012638 DG |
113 | return lks; |
114 | ||
115 | error: | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | /* | |
050349bb | 120 | * Allocate and initialize a kernel channel data structure. |
54012638 | 121 | * |
050349bb | 122 | * Return pointer to structure or NULL. |
54012638 | 123 | */ |
62499ad6 | 124 | struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path) |
54012638 DG |
125 | { |
126 | int ret; | |
127 | struct ltt_kernel_channel *lkc; | |
54012638 | 128 | |
ba7f0ae5 | 129 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
f3ed775e | 130 | if (lkc == NULL) { |
df0f840b | 131 | PERROR("ltt_kernel_channel zmalloc"); |
54012638 DG |
132 | goto error; |
133 | } | |
134 | ||
ba7f0ae5 | 135 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
f3ed775e | 136 | if (lkc->channel == NULL) { |
df0f840b | 137 | PERROR("lttng_channel zmalloc"); |
f3ed775e DG |
138 | goto error; |
139 | } | |
140 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); | |
54012638 | 141 | |
03550b58 | 142 | lkc->fd = -1; |
54012638 | 143 | lkc->stream_count = 0; |
cbbbb275 | 144 | lkc->event_count = 0; |
d36b8583 | 145 | lkc->enabled = 1; |
e75cda3a | 146 | lkc->ctx = NULL; |
54012638 DG |
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 */ | |
b082db07 | 151 | ret = asprintf(&lkc->pathname, "%s", path); |
54012638 | 152 | if (ret < 0) { |
df0f840b | 153 | PERROR("asprintf kernel create channel"); |
54012638 DG |
154 | goto error; |
155 | } | |
156 | ||
157 | return lkc; | |
158 | ||
159 | error: | |
160 | return NULL; | |
161 | } | |
162 | ||
163 | /* | |
050349bb | 164 | * Allocate and initialize a kernel event. Set name and event type. |
54012638 | 165 | * |
050349bb | 166 | * Return pointer to structure or NULL. |
54012638 | 167 | */ |
62499ad6 | 168 | struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev) |
54012638 DG |
169 | { |
170 | struct ltt_kernel_event *lke; | |
171 | struct lttng_kernel_event *attr; | |
172 | ||
ba7f0ae5 DG |
173 | lke = zmalloc(sizeof(struct ltt_kernel_event)); |
174 | attr = zmalloc(sizeof(struct lttng_kernel_event)); | |
54012638 | 175 | if (lke == NULL || attr == NULL) { |
df0f840b | 176 | PERROR("kernel event zmalloc"); |
54012638 DG |
177 | goto error; |
178 | } | |
179 | ||
f3ed775e | 180 | switch (ev->type) { |
7d29a247 | 181 | case LTTNG_EVENT_PROBE: |
e6ddca71 | 182 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
7d29a247 DG |
183 | attr->u.kprobe.addr = ev->attr.probe.addr; |
184 | attr->u.kprobe.offset = ev->attr.probe.offset; | |
f3ed775e | 185 | strncpy(attr->u.kprobe.symbol_name, |
dbbb3ec5 DG |
186 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
187 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e DG |
188 | break; |
189 | case LTTNG_EVENT_FUNCTION: | |
8f0d098b MD |
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, | |
dbbb3ec5 DG |
195 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
196 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
8f0d098b MD |
197 | break; |
198 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
f3ed775e DG |
199 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
200 | strncpy(attr->u.ftrace.symbol_name, | |
dbbb3ec5 DG |
201 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
202 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 203 | break; |
e6ddca71 DG |
204 | case LTTNG_EVENT_TRACEPOINT: |
205 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; | |
f3ed775e | 206 | break; |
a54bd42d MD |
207 | case LTTNG_EVENT_SYSCALL: |
208 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; | |
0133c199 | 209 | break; |
7a3d1328 MD |
210 | case LTTNG_EVENT_ALL: |
211 | attr->instrumentation = LTTNG_KERNEL_ALL; | |
212 | break; | |
f3ed775e DG |
213 | default: |
214 | ERR("Unknown kernel instrumentation type (%d)", ev->type); | |
215 | goto error; | |
216 | } | |
217 | ||
218 | /* Copy event name */ | |
dbbb3ec5 DG |
219 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
220 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 221 | |
54012638 | 222 | /* Setting up a kernel event */ |
03550b58 | 223 | lke->fd = -1; |
54012638 | 224 | lke->event = attr; |
d36b8583 | 225 | lke->enabled = 1; |
e75cda3a | 226 | lke->ctx = NULL; |
54012638 DG |
227 | |
228 | return lke; | |
229 | ||
230 | error: | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | /* | |
050349bb | 235 | * Allocate and initialize a kernel metadata. |
54012638 | 236 | * |
050349bb | 237 | * Return pointer to structure or NULL. |
54012638 | 238 | */ |
62499ad6 | 239 | struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path) |
54012638 DG |
240 | { |
241 | int ret; | |
242 | struct ltt_kernel_metadata *lkm; | |
f3ed775e | 243 | struct lttng_channel *chan; |
54012638 | 244 | |
ba7f0ae5 DG |
245 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
246 | chan = zmalloc(sizeof(struct lttng_channel)); | |
f3ed775e | 247 | if (lkm == NULL || chan == NULL) { |
df0f840b | 248 | PERROR("kernel metadata zmalloc"); |
54012638 DG |
249 | goto error; |
250 | } | |
251 | ||
252 | /* Set default attributes */ | |
f3ed775e | 253 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
b389abbe MD |
254 | chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE; |
255 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; | |
f3ed775e DG |
256 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
257 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
7d452e12 | 258 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
54012638 DG |
259 | |
260 | /* Init metadata */ | |
03550b58 | 261 | lkm->fd = -1; |
f3ed775e | 262 | lkm->conf = chan; |
54012638 | 263 | /* Set default metadata path */ |
58a97671 | 264 | ret = asprintf(&lkm->pathname, "%s/metadata", path); |
54012638 | 265 | if (ret < 0) { |
df0f840b | 266 | PERROR("asprintf kernel metadata"); |
54012638 DG |
267 | goto error; |
268 | } | |
269 | ||
270 | return lkm; | |
271 | ||
272 | error: | |
273 | return NULL; | |
274 | } | |
275 | ||
276 | /* | |
050349bb DG |
277 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
278 | * default. | |
54012638 | 279 | * |
050349bb | 280 | * Return pointer to structure or NULL. |
54012638 | 281 | */ |
62499ad6 | 282 | struct ltt_kernel_stream *trace_kernel_create_stream(void) |
54012638 DG |
283 | { |
284 | struct ltt_kernel_stream *lks; | |
285 | ||
ba7f0ae5 | 286 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
54012638 | 287 | if (lks == NULL) { |
df0f840b | 288 | PERROR("kernel stream zmalloc"); |
54012638 DG |
289 | goto error; |
290 | } | |
291 | ||
292 | /* Init stream */ | |
03550b58 | 293 | lks->fd = -1; |
54012638 DG |
294 | lks->pathname = NULL; |
295 | lks->state = 0; | |
296 | ||
297 | return lks; | |
298 | ||
299 | error: | |
300 | return NULL; | |
301 | } | |
c363b55d | 302 | |
050349bb DG |
303 | /* |
304 | * Cleanup kernel stream structure. | |
305 | */ | |
62499ad6 | 306 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
c363b55d | 307 | { |
799e2c4f MD |
308 | int ret; |
309 | ||
33a2b854 | 310 | DBG("[trace] Closing stream fd %d", stream->fd); |
c363b55d | 311 | /* Close kernel fd */ |
03550b58 MD |
312 | if (stream->fd >= 0) { |
313 | ret = close(stream->fd); | |
314 | if (ret) { | |
315 | PERROR("close"); | |
316 | } | |
799e2c4f | 317 | } |
c363b55d DG |
318 | /* Remove from stream list */ |
319 | cds_list_del(&stream->list); | |
f9815039 DG |
320 | |
321 | free(stream->pathname); | |
c363b55d DG |
322 | free(stream); |
323 | } | |
324 | ||
050349bb DG |
325 | /* |
326 | * Cleanup kernel event structure. | |
327 | */ | |
62499ad6 | 328 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
c363b55d | 329 | { |
799e2c4f MD |
330 | int ret; |
331 | ||
87eb4ab8 MD |
332 | if (event->fd >= 0) { |
333 | DBG("[trace] Closing event fd %d", event->fd); | |
334 | /* Close kernel fd */ | |
799e2c4f MD |
335 | ret = close(event->fd); |
336 | if (ret) { | |
337 | PERROR("close"); | |
338 | } | |
87eb4ab8 MD |
339 | } else { |
340 | DBG("[trace] Tearing down event (no associated fd)"); | |
341 | } | |
c363b55d DG |
342 | |
343 | /* Remove from event list */ | |
344 | cds_list_del(&event->list); | |
f9815039 DG |
345 | |
346 | free(event->event); | |
347 | free(event->ctx); | |
c363b55d DG |
348 | free(event); |
349 | } | |
350 | ||
050349bb DG |
351 | /* |
352 | * Cleanup kernel channel structure. | |
353 | */ | |
62499ad6 | 354 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
c363b55d | 355 | { |
af9737e9 DG |
356 | struct ltt_kernel_stream *stream, *stmp; |
357 | struct ltt_kernel_event *event, *etmp; | |
799e2c4f | 358 | int ret; |
c363b55d | 359 | |
33a2b854 | 360 | DBG("[trace] Closing channel fd %d", channel->fd); |
c363b55d | 361 | /* Close kernel fd */ |
03550b58 MD |
362 | if (channel->fd >= 0) { |
363 | ret = close(channel->fd); | |
364 | if (ret) { | |
365 | PERROR("close"); | |
366 | } | |
799e2c4f | 367 | } |
c363b55d DG |
368 | |
369 | /* For each stream in the channel list */ | |
af9737e9 | 370 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
62499ad6 | 371 | trace_kernel_destroy_stream(stream); |
c363b55d DG |
372 | } |
373 | ||
374 | /* For each event in the channel list */ | |
af9737e9 | 375 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
62499ad6 | 376 | trace_kernel_destroy_event(event); |
c363b55d DG |
377 | } |
378 | ||
379 | /* Remove from channel list */ | |
380 | cds_list_del(&channel->list); | |
f9815039 DG |
381 | |
382 | free(channel->pathname); | |
383 | free(channel->channel); | |
384 | free(channel->ctx); | |
c363b55d DG |
385 | free(channel); |
386 | } | |
387 | ||
050349bb DG |
388 | /* |
389 | * Cleanup kernel metadata structure. | |
390 | */ | |
62499ad6 | 391 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
c363b55d | 392 | { |
799e2c4f MD |
393 | int ret; |
394 | ||
33a2b854 | 395 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
c363b55d | 396 | /* Close kernel fd */ |
03550b58 MD |
397 | if (metadata->fd >= 0) { |
398 | ret = close(metadata->fd); | |
399 | if (ret) { | |
400 | PERROR("close"); | |
401 | } | |
799e2c4f | 402 | } |
c363b55d | 403 | |
f9815039 DG |
404 | free(metadata->conf); |
405 | free(metadata->pathname); | |
c363b55d DG |
406 | free(metadata); |
407 | } | |
408 | ||
050349bb | 409 | /* |
62499ad6 | 410 | * Cleanup kernel session structure |
050349bb | 411 | */ |
62499ad6 | 412 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
c363b55d | 413 | { |
af9737e9 | 414 | struct ltt_kernel_channel *channel, *ctmp; |
799e2c4f | 415 | int ret; |
c363b55d | 416 | |
33a2b854 | 417 | DBG("[trace] Closing session fd %d", session->fd); |
c363b55d | 418 | /* Close kernel fds */ |
03550b58 MD |
419 | if (session->fd >= 0) { |
420 | ret = close(session->fd); | |
421 | if (ret) { | |
422 | PERROR("close"); | |
423 | } | |
799e2c4f | 424 | } |
f9815039 | 425 | |
03550b58 | 426 | if (session->metadata_stream_fd >= 0) { |
70dc1c34 | 427 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
799e2c4f MD |
428 | ret = close(session->metadata_stream_fd); |
429 | if (ret) { | |
430 | PERROR("close"); | |
431 | } | |
70dc1c34 | 432 | } |
c363b55d | 433 | |
d36b8583 | 434 | if (session->metadata != NULL) { |
62499ad6 | 435 | trace_kernel_destroy_metadata(session->metadata); |
d36b8583 | 436 | } |
c363b55d | 437 | |
af9737e9 | 438 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
62499ad6 | 439 | trace_kernel_destroy_channel(channel); |
c363b55d DG |
440 | } |
441 | ||
f9815039 | 442 | free(session->trace_path); |
c363b55d DG |
443 | free(session); |
444 | } |