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