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