6a17776aa4fc10a8337c1aa9ea3383515d9f480a
[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 assert(session);
39 assert(name);
40
41 DBG("Trying to find channel %s", name);
42
43 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
44 if (strcmp(name, chan->channel->name) == 0) {
45 DBG("Found channel by name %s", name);
46 return chan;
47 }
48 }
49
50 return NULL;
51 }
52
53 /*
54 * Find the event name for the given channel.
55 */
56 struct ltt_kernel_event *trace_kernel_get_event_by_name(
57 char *name, struct ltt_kernel_channel *channel)
58 {
59 struct ltt_kernel_event *ev;
60
61 assert(name);
62 assert(channel);
63
64 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
65 if (strcmp(name, ev->event->name) == 0) {
66 DBG("Found event by name %s for channel %s", name,
67 channel->channel->name);
68 return ev;
69 }
70 }
71
72 return NULL;
73 }
74
75 /*
76 * Allocate and initialize a kernel session data structure.
77 *
78 * Return pointer to structure or NULL.
79 */
80 struct ltt_kernel_session *trace_kernel_create_session(char *path)
81 {
82 struct ltt_kernel_session *lks = NULL;
83
84 /* Allocate a new ltt kernel session */
85 lks = zmalloc(sizeof(struct ltt_kernel_session));
86 if (lks == NULL) {
87 PERROR("create kernel session zmalloc");
88 goto alloc_error;
89 }
90
91 /* Init data structure */
92 lks->fd = -1;
93 lks->metadata_stream_fd = -1;
94 lks->channel_count = 0;
95 lks->stream_count_global = 0;
96 lks->metadata = NULL;
97 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
98
99 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
100 if (lks->consumer == NULL) {
101 goto error;
102 }
103
104 /*
105 * The tmp_consumer stays NULL until a set_consumer_uri command is
106 * executed. At this point, the consumer should be nullify until an
107 * enable_consumer command. This assignment is symbolic since we've zmalloc
108 * the struct.
109 */
110 lks->tmp_consumer = NULL;
111
112 if (*path != '\0') {
113 int ret;
114
115 /* Use the default consumer output which is the tracing session path. */
116 ret = snprintf(lks->consumer->dst.trace_path, PATH_MAX,
117 "%s" DEFAULT_KERNEL_TRACE_DIR, path);
118 if (ret < 0) {
119 PERROR("snprintf consumer trace path");
120 goto error;
121 }
122
123 /* Set session path */
124 ret = asprintf(&lks->trace_path, "%s" DEFAULT_KERNEL_TRACE_DIR, path);
125 if (ret < 0) {
126 PERROR("asprintf kernel traces path");
127 goto error;
128 }
129 }
130
131 return lks;
132
133 error:
134 free(lks);
135
136 alloc_error:
137 return NULL;
138 }
139
140 /*
141 * Allocate and initialize a kernel channel data structure.
142 *
143 * Return pointer to structure or NULL.
144 */
145 struct ltt_kernel_channel *trace_kernel_create_channel(
146 struct lttng_channel *chan)
147 {
148 struct ltt_kernel_channel *lkc;
149
150 assert(chan);
151
152 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
153 if (lkc == NULL) {
154 PERROR("ltt_kernel_channel zmalloc");
155 goto error;
156 }
157
158 lkc->channel = zmalloc(sizeof(struct lttng_channel));
159 if (lkc->channel == NULL) {
160 PERROR("lttng_channel zmalloc");
161 free(lkc);
162 goto error;
163 }
164 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
165
166 lkc->fd = -1;
167 lkc->stream_count = 0;
168 lkc->event_count = 0;
169 lkc->enabled = 1;
170 lkc->ctx = NULL;
171 /* Init linked list */
172 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
173 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
174
175 return lkc;
176
177 error:
178 return NULL;
179 }
180
181 /*
182 * Allocate and initialize a kernel event. Set name and event type.
183 *
184 * Return pointer to structure or NULL.
185 */
186 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
187 {
188 struct ltt_kernel_event *lke;
189 struct lttng_kernel_event *attr;
190
191 assert(ev);
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
247 return lke;
248
249 error:
250 free(lke);
251 free(attr);
252 return NULL;
253 }
254
255 /*
256 * Allocate and initialize a kernel metadata.
257 *
258 * Return pointer to structure or NULL.
259 */
260 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
261 {
262 struct ltt_kernel_metadata *lkm;
263 struct lttng_channel *chan;
264
265 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
266 chan = zmalloc(sizeof(struct lttng_channel));
267 if (lkm == NULL || chan == NULL) {
268 PERROR("kernel metadata zmalloc");
269 goto error;
270 }
271
272 /* Set default attributes */
273 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
274 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
275 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
276 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
277 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
278 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
279
280 /* Init metadata */
281 lkm->fd = -1;
282 lkm->conf = chan;
283
284 return lkm;
285
286 error:
287 free(lkm);
288 free(chan);
289 return NULL;
290 }
291
292 /*
293 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
294 * default.
295 *
296 * Return pointer to structure or NULL.
297 */
298 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
299 unsigned int count)
300 {
301 int ret;
302 struct ltt_kernel_stream *lks;
303
304 assert(name);
305
306 lks = zmalloc(sizeof(struct ltt_kernel_stream));
307 if (lks == NULL) {
308 PERROR("kernel stream zmalloc");
309 goto error;
310 }
311
312 /* Set name */
313 ret = snprintf(lks->name, sizeof(lks->name), "%s_%d", name, count);
314 if (ret < 0) {
315 PERROR("snprintf stream name");
316 goto error;
317 }
318 lks->name[sizeof(lks->name) - 1] = '\0';
319
320 /* Init stream */
321 lks->fd = -1;
322 lks->state = 0;
323 lks->cpu = count;
324
325 return lks;
326
327 error:
328 return NULL;
329 }
330
331 /*
332 * Cleanup kernel stream structure.
333 */
334 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
335 {
336 assert(stream);
337
338 DBG("[trace] Closing stream fd %d", stream->fd);
339 /* Close kernel fd */
340 if (stream->fd >= 0) {
341 int ret;
342
343 ret = close(stream->fd);
344 if (ret) {
345 PERROR("close");
346 }
347 }
348 /* Remove from stream list */
349 cds_list_del(&stream->list);
350
351 free(stream);
352 }
353
354 /*
355 * Cleanup kernel event structure.
356 */
357 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
358 {
359 assert(event);
360
361 if (event->fd >= 0) {
362 int ret;
363
364 DBG("[trace] Closing event fd %d", event->fd);
365 /* Close kernel fd */
366 ret = close(event->fd);
367 if (ret) {
368 PERROR("close");
369 }
370 } else {
371 DBG("[trace] Tearing down event (no associated fd)");
372 }
373
374 /* Remove from event list */
375 cds_list_del(&event->list);
376
377 free(event->event);
378 free(event);
379 }
380
381 /*
382 * Cleanup kernel channel structure.
383 */
384 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
385 {
386 struct ltt_kernel_stream *stream, *stmp;
387 struct ltt_kernel_event *event, *etmp;
388 int ret;
389
390 assert(channel);
391
392 DBG("[trace] Closing channel fd %d", channel->fd);
393 /* Close kernel fd */
394 if (channel->fd >= 0) {
395 ret = close(channel->fd);
396 if (ret) {
397 PERROR("close");
398 }
399 }
400
401 /* For each stream in the channel list */
402 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
403 trace_kernel_destroy_stream(stream);
404 }
405
406 /* For each event in the channel list */
407 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
408 trace_kernel_destroy_event(event);
409 }
410
411 /* Remove from channel list */
412 cds_list_del(&channel->list);
413
414 free(channel->channel);
415 free(channel->ctx);
416 free(channel);
417 }
418
419 /*
420 * Cleanup kernel metadata structure.
421 */
422 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
423 {
424 assert(metadata);
425
426 DBG("[trace] Closing metadata fd %d", metadata->fd);
427 /* Close kernel fd */
428 if (metadata->fd >= 0) {
429 int ret;
430
431 ret = close(metadata->fd);
432 if (ret) {
433 PERROR("close");
434 }
435 }
436
437 free(metadata->conf);
438 free(metadata);
439 }
440
441 /*
442 * Cleanup kernel session structure
443 */
444 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
445 {
446 struct ltt_kernel_channel *channel, *ctmp;
447 int ret;
448
449 assert(session);
450
451 DBG("[trace] Closing session fd %d", session->fd);
452 /* Close kernel fds */
453 if (session->fd >= 0) {
454 ret = close(session->fd);
455 if (ret) {
456 PERROR("close");
457 }
458 }
459
460 if (session->metadata_stream_fd >= 0) {
461 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
462 ret = close(session->metadata_stream_fd);
463 if (ret) {
464 PERROR("close");
465 }
466 }
467
468 if (session->metadata != NULL) {
469 trace_kernel_destroy_metadata(session->metadata);
470 }
471
472 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
473 trace_kernel_destroy_channel(channel);
474 }
475
476 /* Wipe consumer output object */
477 consumer_destroy_output(session->consumer);
478 consumer_destroy_output(session->tmp_consumer);
479
480 free(session->trace_path);
481 free(session);
482 }
This page took 0.038117 seconds and 3 git commands to generate.