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