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