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