Commit | Line | Data |
---|---|---|
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 DG |
24 | #include <urcu/list.h> |
25 | ||
33a2b854 | 26 | #include "lttngerr.h" |
54012638 DG |
27 | #include "trace.h" |
28 | ||
19e70852 DG |
29 | /* |
30 | * get_kernel_channel_by_name | |
31 | * | |
32 | * Find the channel name for the given kernel session. | |
33 | */ | |
34 | struct ltt_kernel_channel *get_kernel_channel_by_name( | |
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 | ||
44 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { | |
45 | if (strcmp(name, chan->channel->name) == 0) { | |
46 | DBG("Found channel by name %s", name); | |
47 | return chan; | |
48 | } | |
49 | } | |
50 | ||
51 | error: | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | /* | |
56 | * get_kernel_event_by_name | |
57 | * | |
58 | * Find the event name for the given channel. | |
59 | */ | |
60 | struct ltt_kernel_event *get_kernel_event_by_name( | |
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 | ||
78 | error: | |
79 | return NULL; | |
80 | } | |
81 | ||
54012638 DG |
82 | /* |
83 | * trace_create_kernel_session | |
84 | * | |
85 | * Allocate and initialize a kernel session data structure. | |
86 | * | |
87 | * Return pointer to structure or NULL. | |
88 | */ | |
89 | struct ltt_kernel_session *trace_create_kernel_session(void) | |
90 | { | |
91 | struct ltt_kernel_session *lks; | |
92 | ||
93 | /* Allocate a new ltt kernel session */ | |
94 | lks = malloc(sizeof(struct ltt_kernel_session)); | |
95 | if (lks == NULL) { | |
96 | perror("create kernel session malloc"); | |
97 | goto error; | |
98 | } | |
99 | ||
100 | /* Init data structure */ | |
101 | lks->fd = 0; | |
102 | lks->metadata_stream_fd = 0; | |
103 | lks->channel_count = 0; | |
104 | lks->stream_count_global = 0; | |
105 | lks->metadata = NULL; | |
106 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); | |
107 | ||
108 | return lks; | |
109 | ||
110 | error: | |
111 | return NULL; | |
112 | } | |
113 | ||
114 | /* | |
115 | * trace_create_kernel_channel | |
116 | * | |
117 | * Allocate and initialize a kernel channel data structure. | |
118 | * | |
119 | * Return pointer to structure or NULL. | |
120 | */ | |
b082db07 | 121 | struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path) |
54012638 DG |
122 | { |
123 | int ret; | |
124 | struct ltt_kernel_channel *lkc; | |
54012638 DG |
125 | |
126 | lkc = malloc(sizeof(struct ltt_kernel_channel)); | |
f3ed775e DG |
127 | if (lkc == NULL) { |
128 | perror("ltt_kernel_channel malloc"); | |
54012638 DG |
129 | goto error; |
130 | } | |
131 | ||
f3ed775e DG |
132 | lkc->channel = malloc(sizeof(struct lttng_channel)); |
133 | if (lkc->channel == NULL) { | |
134 | perror("lttng_channel malloc"); | |
135 | goto error; | |
136 | } | |
137 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); | |
54012638 DG |
138 | |
139 | lkc->fd = 0; | |
140 | lkc->stream_count = 0; | |
d36b8583 | 141 | lkc->enabled = 1; |
e75cda3a | 142 | lkc->ctx = NULL; |
54012638 DG |
143 | /* Init linked list */ |
144 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); | |
145 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); | |
146 | /* Set default trace output path */ | |
b082db07 | 147 | ret = asprintf(&lkc->pathname, "%s", path); |
54012638 DG |
148 | if (ret < 0) { |
149 | perror("asprintf kernel create channel"); | |
150 | goto error; | |
151 | } | |
152 | ||
153 | return lkc; | |
154 | ||
155 | error: | |
156 | return NULL; | |
157 | } | |
158 | ||
159 | /* | |
160 | * trace_create_kernel_event | |
161 | * | |
162 | * Allocate and initialize a kernel event. Set name and event type. | |
163 | * | |
164 | * Return pointer to structure or NULL. | |
165 | */ | |
f3ed775e | 166 | struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev) |
54012638 DG |
167 | { |
168 | struct ltt_kernel_event *lke; | |
169 | struct lttng_kernel_event *attr; | |
170 | ||
171 | lke = malloc(sizeof(struct ltt_kernel_event)); | |
172 | attr = malloc(sizeof(struct lttng_kernel_event)); | |
173 | if (lke == NULL || attr == NULL) { | |
174 | perror("kernel event malloc"); | |
175 | goto error; | |
176 | } | |
177 | ||
f3ed775e | 178 | switch (ev->type) { |
7d29a247 | 179 | case LTTNG_EVENT_PROBE: |
e6ddca71 | 180 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
7d29a247 DG |
181 | attr->u.kprobe.addr = ev->attr.probe.addr; |
182 | attr->u.kprobe.offset = ev->attr.probe.offset; | |
f3ed775e | 183 | strncpy(attr->u.kprobe.symbol_name, |
7d29a247 | 184 | ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN); |
f3ed775e DG |
185 | break; |
186 | case LTTNG_EVENT_FUNCTION: | |
187 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; | |
188 | strncpy(attr->u.ftrace.symbol_name, | |
189 | ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN); | |
190 | break; | |
e6ddca71 DG |
191 | case LTTNG_EVENT_TRACEPOINT: |
192 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; | |
f3ed775e DG |
193 | break; |
194 | default: | |
195 | ERR("Unknown kernel instrumentation type (%d)", ev->type); | |
196 | goto error; | |
197 | } | |
198 | ||
199 | /* Copy event name */ | |
200 | strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN); | |
201 | ||
54012638 DG |
202 | /* Setting up a kernel event */ |
203 | lke->fd = 0; | |
204 | lke->event = attr; | |
d36b8583 | 205 | lke->enabled = 1; |
e75cda3a | 206 | lke->ctx = NULL; |
54012638 DG |
207 | |
208 | return lke; | |
209 | ||
210 | error: | |
211 | return NULL; | |
212 | } | |
213 | ||
214 | /* | |
215 | * trace_create_kernel_metadata | |
216 | * | |
217 | * Allocate and initialize a kernel metadata. | |
218 | * | |
219 | * Return pointer to structure or NULL. | |
220 | */ | |
58a97671 | 221 | struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path) |
54012638 DG |
222 | { |
223 | int ret; | |
224 | struct ltt_kernel_metadata *lkm; | |
f3ed775e | 225 | struct lttng_channel *chan; |
54012638 DG |
226 | |
227 | lkm = malloc(sizeof(struct ltt_kernel_metadata)); | |
f3ed775e DG |
228 | chan = malloc(sizeof(struct lttng_channel)); |
229 | if (lkm == NULL || chan == NULL) { | |
54012638 DG |
230 | perror("kernel metadata malloc"); |
231 | goto error; | |
232 | } | |
233 | ||
234 | /* Set default attributes */ | |
f3ed775e DG |
235 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
236 | chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE; | |
237 | chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM; | |
238 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
239 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
7d452e12 | 240 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
54012638 DG |
241 | |
242 | /* Init metadata */ | |
243 | lkm->fd = 0; | |
f3ed775e | 244 | lkm->conf = chan; |
54012638 | 245 | /* Set default metadata path */ |
58a97671 | 246 | ret = asprintf(&lkm->pathname, "%s/metadata", path); |
54012638 DG |
247 | if (ret < 0) { |
248 | perror("asprintf kernel metadata"); | |
249 | goto error; | |
250 | } | |
251 | ||
252 | return lkm; | |
253 | ||
254 | error: | |
255 | return NULL; | |
256 | } | |
257 | ||
258 | /* | |
259 | * trace_create_kernel_stream | |
260 | * | |
261 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by | |
262 | * default. | |
263 | * | |
264 | * Return pointer to structure or NULL. | |
265 | */ | |
266 | struct ltt_kernel_stream *trace_create_kernel_stream(void) | |
267 | { | |
268 | struct ltt_kernel_stream *lks; | |
269 | ||
270 | lks = malloc(sizeof(struct ltt_kernel_stream)); | |
271 | if (lks == NULL) { | |
272 | perror("kernel stream malloc"); | |
273 | goto error; | |
274 | } | |
275 | ||
276 | /* Init stream */ | |
277 | lks->fd = 0; | |
278 | lks->pathname = NULL; | |
279 | lks->state = 0; | |
280 | ||
281 | return lks; | |
282 | ||
283 | error: | |
284 | return NULL; | |
285 | } | |
c363b55d DG |
286 | |
287 | void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream) | |
288 | { | |
33a2b854 | 289 | DBG("[trace] Closing stream fd %d", stream->fd); |
c363b55d DG |
290 | /* Close kernel fd */ |
291 | close(stream->fd); | |
292 | free(stream->pathname); | |
293 | ||
294 | /* Remove from stream list */ | |
295 | cds_list_del(&stream->list); | |
296 | free(stream); | |
297 | } | |
298 | ||
299 | void trace_destroy_kernel_event(struct ltt_kernel_event *event) | |
300 | { | |
33a2b854 | 301 | DBG("[trace] Closing event fd %d", event->fd); |
c363b55d DG |
302 | /* Close kernel fd */ |
303 | close(event->fd); | |
304 | /* Free attributes */ | |
305 | free(event->event); | |
306 | ||
307 | /* Remove from event list */ | |
308 | cds_list_del(&event->list); | |
309 | free(event); | |
310 | } | |
311 | ||
312 | void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel) | |
313 | { | |
314 | struct ltt_kernel_stream *stream; | |
315 | struct ltt_kernel_event *event; | |
316 | ||
33a2b854 | 317 | DBG("[trace] Closing channel fd %d", channel->fd); |
c363b55d DG |
318 | /* Close kernel fd */ |
319 | close(channel->fd); | |
320 | free(channel->pathname); | |
321 | /* Free attributes structure */ | |
322 | free(channel->channel); | |
323 | ||
324 | /* For each stream in the channel list */ | |
325 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
326 | trace_destroy_kernel_stream(stream); | |
327 | } | |
328 | ||
329 | /* For each event in the channel list */ | |
330 | cds_list_for_each_entry(event, &channel->events_list.head, list) { | |
331 | trace_destroy_kernel_event(event); | |
332 | } | |
333 | ||
334 | /* Remove from channel list */ | |
335 | cds_list_del(&channel->list); | |
336 | free(channel); | |
337 | } | |
338 | ||
339 | void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata) | |
340 | { | |
33a2b854 | 341 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
c363b55d DG |
342 | /* Close kernel fd */ |
343 | close(metadata->fd); | |
344 | /* Free attributes */ | |
345 | free(metadata->conf); | |
346 | ||
347 | free(metadata); | |
348 | } | |
349 | ||
350 | void trace_destroy_kernel_session(struct ltt_kernel_session *session) | |
351 | { | |
352 | struct ltt_kernel_channel *channel; | |
353 | ||
33a2b854 | 354 | DBG("[trace] Closing session fd %d", session->fd); |
c363b55d DG |
355 | /* Close kernel fds */ |
356 | close(session->fd); | |
70dc1c34 DG |
357 | if (session->metadata_stream_fd != 0) { |
358 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); | |
359 | close(session->metadata_stream_fd); | |
360 | } | |
c363b55d | 361 | |
d36b8583 DG |
362 | if (session->metadata != NULL) { |
363 | trace_destroy_kernel_metadata(session->metadata); | |
364 | } | |
c363b55d DG |
365 | |
366 | cds_list_for_each_entry(channel, &session->channel_list.head, list) { | |
367 | trace_destroy_kernel_channel(channel); | |
368 | } | |
369 | ||
370 | free(session); | |
371 | } |