Remove unused variables in trace-kernel/ust.c
[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 && strlen(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 goto error;
162 }
163 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
164
165 lkc->fd = -1;
166 lkc->stream_count = 0;
167 lkc->event_count = 0;
168 lkc->enabled = 1;
169 lkc->ctx = NULL;
170 /* Init linked list */
171 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
172 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
173
174 return lkc;
175
176 error:
177 return NULL;
178 }
179
180 /*
181 * Allocate and initialize a kernel event. Set name and event type.
182 *
183 * Return pointer to structure or NULL.
184 */
185 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
186 {
187 struct ltt_kernel_event *lke;
188 struct lttng_kernel_event *attr;
189
190 assert(ev);
191
192 lke = zmalloc(sizeof(struct ltt_kernel_event));
193 attr = zmalloc(sizeof(struct lttng_kernel_event));
194 if (lke == NULL || attr == NULL) {
195 PERROR("kernel event zmalloc");
196 goto error;
197 }
198
199 switch (ev->type) {
200 case LTTNG_EVENT_PROBE:
201 attr->instrumentation = LTTNG_KERNEL_KPROBE;
202 attr->u.kprobe.addr = ev->attr.probe.addr;
203 attr->u.kprobe.offset = ev->attr.probe.offset;
204 strncpy(attr->u.kprobe.symbol_name,
205 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
206 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
207 break;
208 case LTTNG_EVENT_FUNCTION:
209 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
210 attr->u.kretprobe.addr = ev->attr.probe.addr;
211 attr->u.kretprobe.offset = ev->attr.probe.offset;
212 attr->u.kretprobe.offset = ev->attr.probe.offset;
213 strncpy(attr->u.kretprobe.symbol_name,
214 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
215 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
216 break;
217 case LTTNG_EVENT_FUNCTION_ENTRY:
218 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
219 strncpy(attr->u.ftrace.symbol_name,
220 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
221 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
222 break;
223 case LTTNG_EVENT_TRACEPOINT:
224 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
225 break;
226 case LTTNG_EVENT_SYSCALL:
227 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
228 break;
229 case LTTNG_EVENT_ALL:
230 attr->instrumentation = LTTNG_KERNEL_ALL;
231 break;
232 default:
233 ERR("Unknown kernel instrumentation type (%d)", ev->type);
234 goto error;
235 }
236
237 /* Copy event name */
238 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
239 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
240
241 /* Setting up a kernel event */
242 lke->fd = -1;
243 lke->event = attr;
244 lke->enabled = 1;
245
246 return lke;
247
248 error:
249 free(lke);
250 free(attr);
251 return NULL;
252 }
253
254 /*
255 * Allocate and initialize a kernel metadata.
256 *
257 * Return pointer to structure or NULL.
258 */
259 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
260 {
261 struct ltt_kernel_metadata *lkm;
262 struct lttng_channel *chan;
263
264 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
265 chan = zmalloc(sizeof(struct lttng_channel));
266 if (lkm == NULL || chan == NULL) {
267 PERROR("kernel metadata zmalloc");
268 goto error;
269 }
270
271 /* Set default attributes */
272 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
273 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
274 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
275 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
276 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
277 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
278
279 /* Init metadata */
280 lkm->fd = -1;
281 lkm->conf = chan;
282
283 return lkm;
284
285 error:
286 free(lkm);
287 free(chan);
288 return NULL;
289 }
290
291 /*
292 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
293 * default.
294 *
295 * Return pointer to structure or NULL.
296 */
297 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
298 unsigned int count)
299 {
300 int ret;
301 struct ltt_kernel_stream *lks;
302
303 assert(name);
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 assert(stream);
335
336 DBG("[trace] Closing stream fd %d", stream->fd);
337 /* Close kernel fd */
338 if (stream->fd >= 0) {
339 int ret;
340
341 ret = close(stream->fd);
342 if (ret) {
343 PERROR("close");
344 }
345 }
346 /* Remove from stream list */
347 cds_list_del(&stream->list);
348
349 free(stream);
350 }
351
352 /*
353 * Cleanup kernel event structure.
354 */
355 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
356 {
357 assert(event);
358
359 if (event->fd >= 0) {
360 int ret;
361
362 DBG("[trace] Closing event fd %d", event->fd);
363 /* Close kernel fd */
364 ret = close(event->fd);
365 if (ret) {
366 PERROR("close");
367 }
368 } else {
369 DBG("[trace] Tearing down event (no associated fd)");
370 }
371
372 /* Remove from event list */
373 cds_list_del(&event->list);
374
375 free(event->event);
376 free(event);
377 }
378
379 /*
380 * Cleanup kernel channel structure.
381 */
382 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
383 {
384 struct ltt_kernel_stream *stream, *stmp;
385 struct ltt_kernel_event *event, *etmp;
386 int ret;
387
388 assert(channel);
389
390 DBG("[trace] Closing channel fd %d", channel->fd);
391 /* Close kernel fd */
392 if (channel->fd >= 0) {
393 ret = close(channel->fd);
394 if (ret) {
395 PERROR("close");
396 }
397 }
398
399 /* For each stream in the channel list */
400 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
401 trace_kernel_destroy_stream(stream);
402 }
403
404 /* For each event in the channel list */
405 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
406 trace_kernel_destroy_event(event);
407 }
408
409 /* Remove from channel list */
410 cds_list_del(&channel->list);
411
412 free(channel->channel);
413 free(channel->ctx);
414 free(channel);
415 }
416
417 /*
418 * Cleanup kernel metadata structure.
419 */
420 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
421 {
422 assert(metadata);
423
424 DBG("[trace] Closing metadata fd %d", metadata->fd);
425 /* Close kernel fd */
426 if (metadata->fd >= 0) {
427 int ret;
428
429 ret = close(metadata->fd);
430 if (ret) {
431 PERROR("close");
432 }
433 }
434
435 free(metadata->conf);
436 free(metadata);
437 }
438
439 /*
440 * Cleanup kernel session structure
441 */
442 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
443 {
444 struct ltt_kernel_channel *channel, *ctmp;
445 int ret;
446
447 assert(session);
448
449 DBG("[trace] Closing session fd %d", session->fd);
450 /* Close kernel fds */
451 if (session->fd >= 0) {
452 ret = close(session->fd);
453 if (ret) {
454 PERROR("close");
455 }
456 }
457
458 if (session->metadata_stream_fd >= 0) {
459 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
460 ret = close(session->metadata_stream_fd);
461 if (ret) {
462 PERROR("close");
463 }
464 }
465
466 if (session->metadata != NULL) {
467 trace_kernel_destroy_metadata(session->metadata);
468 }
469
470 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
471 trace_kernel_destroy_channel(channel);
472 }
473
474 /* Wipe consumer output object */
475 consumer_destroy_output(session->consumer);
476 consumer_destroy_output(session->tmp_consumer);
477
478 free(session->trace_path);
479 free(session);
480 }
This page took 0.041012 seconds and 6 git commands to generate.