Split and remove lttng-share header file
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
CommitLineData
54012638
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
54012638
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
c363b55d 23#include <unistd.h>
54012638 24
10a8a223 25#include <common/lttngerr.h>
990570ed
DG
26#include <common/common.h>
27#include <common/defaults.h>
1e307fab 28
62499ad6 29#include "trace-kernel.h"
54012638 30
19e70852 31/*
050349bb 32 * Find the channel name for the given kernel session.
19e70852 33 */
62499ad6 34struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
19e70852
DG
35 char *name, struct ltt_kernel_session *session)
36{
37 struct ltt_kernel_channel *chan;
38
39 if (session == NULL) {
40 ERR("Undefine session");
41 goto error;
42 }
43
54d01ffb
DG
44 DBG("Trying to find channel %s", name);
45
19e70852
DG
46 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
47 if (strcmp(name, chan->channel->name) == 0) {
48 DBG("Found channel by name %s", name);
49 return chan;
50 }
51 }
52
53error:
54 return NULL;
55}
56
57/*
050349bb 58 * Find the event name for the given channel.
19e70852 59 */
62499ad6 60struct ltt_kernel_event *trace_kernel_get_event_by_name(
19e70852
DG
61 char *name, struct ltt_kernel_channel *channel)
62{
63 struct ltt_kernel_event *ev;
64
65 if (channel == NULL) {
66 ERR("Undefine channel");
67 goto error;
68 }
69
70 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
71 if (strcmp(name, ev->event->name) == 0) {
72 DBG("Found event by name %s for channel %s", name,
73 channel->channel->name);
74 return ev;
75 }
76 }
77
78error:
79 return NULL;
80}
81
54012638 82/*
050349bb 83 * Allocate and initialize a kernel session data structure.
54012638 84 *
050349bb 85 * Return pointer to structure or NULL.
54012638 86 */
f9815039 87struct ltt_kernel_session *trace_kernel_create_session(char *path)
54012638 88{
f9815039 89 int ret;
54012638
DG
90 struct ltt_kernel_session *lks;
91
92 /* Allocate a new ltt kernel session */
ba7f0ae5 93 lks = zmalloc(sizeof(struct ltt_kernel_session));
54012638 94 if (lks == NULL) {
ba7f0ae5 95 perror("create kernel session zmalloc");
54012638
DG
96 goto error;
97 }
98
99 /* Init data structure */
100 lks->fd = 0;
101 lks->metadata_stream_fd = 0;
102 lks->channel_count = 0;
103 lks->stream_count_global = 0;
104 lks->metadata = NULL;
d9800920 105 lks->consumer_fd = 0;
54012638
DG
106 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
107
f9815039
DG
108 /* Set session path */
109 ret = asprintf(&lks->trace_path, "%s/kernel", path);
110 if (ret < 0) {
111 perror("asprintf kernel traces path");
112 goto error;
113 }
114
54012638
DG
115 return lks;
116
117error:
118 return NULL;
119}
120
121/*
050349bb 122 * Allocate and initialize a kernel channel data structure.
54012638 123 *
050349bb 124 * Return pointer to structure or NULL.
54012638 125 */
62499ad6 126struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
54012638
DG
127{
128 int ret;
129 struct ltt_kernel_channel *lkc;
54012638 130
ba7f0ae5 131 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 132 if (lkc == NULL) {
ba7f0ae5 133 perror("ltt_kernel_channel zmalloc");
54012638
DG
134 goto error;
135 }
136
ba7f0ae5 137 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 138 if (lkc->channel == NULL) {
ba7f0ae5 139 perror("lttng_channel zmalloc");
f3ed775e
DG
140 goto error;
141 }
142 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
143
144 lkc->fd = 0;
145 lkc->stream_count = 0;
cbbbb275 146 lkc->event_count = 0;
d36b8583 147 lkc->enabled = 1;
e75cda3a 148 lkc->ctx = NULL;
54012638
DG
149 /* Init linked list */
150 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
151 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
152 /* Set default trace output path */
b082db07 153 ret = asprintf(&lkc->pathname, "%s", path);
54012638
DG
154 if (ret < 0) {
155 perror("asprintf kernel create channel");
156 goto error;
157 }
158
159 return lkc;
160
161error:
162 return NULL;
163}
164
165/*
050349bb 166 * Allocate and initialize a kernel event. Set name and event type.
54012638 167 *
050349bb 168 * Return pointer to structure or NULL.
54012638 169 */
62499ad6 170struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
171{
172 struct ltt_kernel_event *lke;
173 struct lttng_kernel_event *attr;
174
ba7f0ae5
DG
175 lke = zmalloc(sizeof(struct ltt_kernel_event));
176 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 177 if (lke == NULL || attr == NULL) {
ba7f0ae5 178 perror("kernel event zmalloc");
54012638
DG
179 goto error;
180 }
181
f3ed775e 182 switch (ev->type) {
7d29a247 183 case LTTNG_EVENT_PROBE:
e6ddca71 184 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
185 attr->u.kprobe.addr = ev->attr.probe.addr;
186 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 187 strncpy(attr->u.kprobe.symbol_name,
7d29a247 188 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 189 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
190 break;
191 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
192 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
193 attr->u.kretprobe.addr = ev->attr.probe.addr;
194 attr->u.kretprobe.offset = ev->attr.probe.offset;
195 attr->u.kretprobe.offset = ev->attr.probe.offset;
196 strncpy(attr->u.kretprobe.symbol_name,
197 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 198 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
199 break;
200 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
201 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
202 strncpy(attr->u.ftrace.symbol_name,
203 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 204 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 205 break;
e6ddca71
DG
206 case LTTNG_EVENT_TRACEPOINT:
207 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 208 break;
a54bd42d
MD
209 case LTTNG_EVENT_SYSCALL:
210 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 211 break;
7a3d1328
MD
212 case LTTNG_EVENT_ALL:
213 attr->instrumentation = LTTNG_KERNEL_ALL;
214 break;
f3ed775e
DG
215 default:
216 ERR("Unknown kernel instrumentation type (%d)", ev->type);
217 goto error;
218 }
219
220 /* Copy event name */
221 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
99497cd0 222 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 223
54012638
DG
224 /* Setting up a kernel event */
225 lke->fd = 0;
226 lke->event = attr;
d36b8583 227 lke->enabled = 1;
e75cda3a 228 lke->ctx = NULL;
54012638
DG
229
230 return lke;
231
232error:
233 return NULL;
234}
235
236/*
050349bb 237 * Allocate and initialize a kernel metadata.
54012638 238 *
050349bb 239 * Return pointer to structure or NULL.
54012638 240 */
62499ad6 241struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
54012638
DG
242{
243 int ret;
244 struct ltt_kernel_metadata *lkm;
f3ed775e 245 struct lttng_channel *chan;
54012638 246
ba7f0ae5
DG
247 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
248 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 249 if (lkm == NULL || chan == NULL) {
ba7f0ae5 250 perror("kernel metadata zmalloc");
54012638
DG
251 goto error;
252 }
253
254 /* Set default attributes */
f3ed775e 255 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
b389abbe
MD
256 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
257 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
258 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
259 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 260 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
261
262 /* Init metadata */
263 lkm->fd = 0;
f3ed775e 264 lkm->conf = chan;
54012638 265 /* Set default metadata path */
58a97671 266 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638
DG
267 if (ret < 0) {
268 perror("asprintf kernel metadata");
269 goto error;
270 }
271
272 return lkm;
273
274error:
275 return NULL;
276}
277
278/*
050349bb
DG
279 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
280 * default.
54012638 281 *
050349bb 282 * Return pointer to structure or NULL.
54012638 283 */
62499ad6 284struct ltt_kernel_stream *trace_kernel_create_stream(void)
54012638
DG
285{
286 struct ltt_kernel_stream *lks;
287
ba7f0ae5 288 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 289 if (lks == NULL) {
ba7f0ae5 290 perror("kernel stream zmalloc");
54012638
DG
291 goto error;
292 }
293
294 /* Init stream */
295 lks->fd = 0;
296 lks->pathname = NULL;
297 lks->state = 0;
298
299 return lks;
300
301error:
302 return NULL;
303}
c363b55d 304
050349bb
DG
305/*
306 * Cleanup kernel stream structure.
307 */
62499ad6 308void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 309{
33a2b854 310 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d
DG
311 /* Close kernel fd */
312 close(stream->fd);
c363b55d
DG
313 /* Remove from stream list */
314 cds_list_del(&stream->list);
f9815039
DG
315
316 free(stream->pathname);
c363b55d
DG
317 free(stream);
318}
319
050349bb
DG
320/*
321 * Cleanup kernel event structure.
322 */
62499ad6 323void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 324{
33a2b854 325 DBG("[trace] Closing event fd %d", event->fd);
c363b55d
DG
326 /* Close kernel fd */
327 close(event->fd);
c363b55d
DG
328
329 /* Remove from event list */
330 cds_list_del(&event->list);
f9815039
DG
331
332 free(event->event);
333 free(event->ctx);
c363b55d
DG
334 free(event);
335}
336
050349bb
DG
337/*
338 * Cleanup kernel channel structure.
339 */
62499ad6 340void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 341{
af9737e9
DG
342 struct ltt_kernel_stream *stream, *stmp;
343 struct ltt_kernel_event *event, *etmp;
c363b55d 344
33a2b854 345 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d
DG
346 /* Close kernel fd */
347 close(channel->fd);
c363b55d
DG
348
349 /* For each stream in the channel list */
af9737e9 350 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 351 trace_kernel_destroy_stream(stream);
c363b55d
DG
352 }
353
354 /* For each event in the channel list */
af9737e9 355 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 356 trace_kernel_destroy_event(event);
c363b55d
DG
357 }
358
359 /* Remove from channel list */
360 cds_list_del(&channel->list);
f9815039
DG
361
362 free(channel->pathname);
363 free(channel->channel);
364 free(channel->ctx);
c363b55d
DG
365 free(channel);
366}
367
050349bb
DG
368/*
369 * Cleanup kernel metadata structure.
370 */
62499ad6 371void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 372{
33a2b854 373 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d
DG
374 /* Close kernel fd */
375 close(metadata->fd);
c363b55d 376
f9815039
DG
377 free(metadata->conf);
378 free(metadata->pathname);
c363b55d
DG
379 free(metadata);
380}
381
050349bb 382/*
62499ad6 383 * Cleanup kernel session structure
050349bb 384 */
62499ad6 385void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 386{
af9737e9 387 struct ltt_kernel_channel *channel, *ctmp;
c363b55d 388
33a2b854 389 DBG("[trace] Closing session fd %d", session->fd);
c363b55d
DG
390 /* Close kernel fds */
391 close(session->fd);
f9815039 392
70dc1c34
DG
393 if (session->metadata_stream_fd != 0) {
394 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
395 close(session->metadata_stream_fd);
396 }
c363b55d 397
d36b8583 398 if (session->metadata != NULL) {
62499ad6 399 trace_kernel_destroy_metadata(session->metadata);
d36b8583 400 }
c363b55d 401
af9737e9 402 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 403 trace_kernel_destroy_channel(channel);
c363b55d
DG
404 }
405
f9815039 406 free(session->trace_path);
c363b55d
DG
407 free(session);
408}
This page took 0.045307 seconds and 4 git commands to generate.