Add kretprobe support (new --function implementation)
[lttng-tools.git] / ltt-sessiond / trace.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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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>
23 #include <unistd.h>
24 #include <urcu/list.h>
25
26 #include "lttngerr.h"
27 #include "trace.h"
28
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
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 */
121 struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
122 {
123 int ret;
124 struct ltt_kernel_channel *lkc;
125
126 lkc = malloc(sizeof(struct ltt_kernel_channel));
127 if (lkc == NULL) {
128 perror("ltt_kernel_channel malloc");
129 goto error;
130 }
131
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));
138
139 lkc->fd = 0;
140 lkc->stream_count = 0;
141 lkc->event_count = 0;
142 lkc->enabled = 1;
143 lkc->ctx = NULL;
144 /* Init linked list */
145 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
146 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
147 /* Set default trace output path */
148 ret = asprintf(&lkc->pathname, "%s", path);
149 if (ret < 0) {
150 perror("asprintf kernel create channel");
151 goto error;
152 }
153
154 return lkc;
155
156 error:
157 return NULL;
158 }
159
160 /*
161 * trace_create_kernel_event
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_create_kernel_event(struct lttng_event *ev)
168 {
169 struct ltt_kernel_event *lke;
170 struct lttng_kernel_event *attr;
171
172 lke = malloc(sizeof(struct ltt_kernel_event));
173 attr = malloc(sizeof(struct lttng_kernel_event));
174 if (lke == NULL || attr == NULL) {
175 perror("kernel event malloc");
176 goto error;
177 }
178
179 switch (ev->type) {
180 case LTTNG_EVENT_PROBE:
181 attr->instrumentation = LTTNG_KERNEL_KPROBE;
182 attr->u.kprobe.addr = ev->attr.probe.addr;
183 attr->u.kprobe.offset = ev->attr.probe.offset;
184 strncpy(attr->u.kprobe.symbol_name,
185 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
186 break;
187 case LTTNG_EVENT_FUNCTION:
188 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
189 attr->u.kretprobe.addr = ev->attr.probe.addr;
190 attr->u.kretprobe.offset = ev->attr.probe.offset;
191 attr->u.kretprobe.offset = ev->attr.probe.offset;
192 strncpy(attr->u.kretprobe.symbol_name,
193 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
194 break;
195 case LTTNG_EVENT_FUNCTION_ENTRY:
196 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
197 strncpy(attr->u.ftrace.symbol_name,
198 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
199 break;
200 case LTTNG_EVENT_TRACEPOINT:
201 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
202 break;
203 default:
204 ERR("Unknown kernel instrumentation type (%d)", ev->type);
205 goto error;
206 }
207
208 /* Copy event name */
209 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
210
211 /* Setting up a kernel event */
212 lke->fd = 0;
213 lke->event = attr;
214 lke->enabled = 1;
215 lke->ctx = NULL;
216
217 return lke;
218
219 error:
220 return NULL;
221 }
222
223 /*
224 * trace_create_kernel_metadata
225 *
226 * Allocate and initialize a kernel metadata.
227 *
228 * Return pointer to structure or NULL.
229 */
230 struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
231 {
232 int ret;
233 struct ltt_kernel_metadata *lkm;
234 struct lttng_channel *chan;
235
236 lkm = malloc(sizeof(struct ltt_kernel_metadata));
237 chan = malloc(sizeof(struct lttng_channel));
238 if (lkm == NULL || chan == NULL) {
239 perror("kernel metadata malloc");
240 goto error;
241 }
242
243 /* Set default attributes */
244 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
245 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
246 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
247 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
248 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
249 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
250
251 /* Init metadata */
252 lkm->fd = 0;
253 lkm->conf = chan;
254 /* Set default metadata path */
255 ret = asprintf(&lkm->pathname, "%s/metadata", path);
256 if (ret < 0) {
257 perror("asprintf kernel metadata");
258 goto error;
259 }
260
261 return lkm;
262
263 error:
264 return NULL;
265 }
266
267 /*
268 * trace_create_kernel_stream
269 *
270 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
271 * default.
272 *
273 * Return pointer to structure or NULL.
274 */
275 struct ltt_kernel_stream *trace_create_kernel_stream(void)
276 {
277 struct ltt_kernel_stream *lks;
278
279 lks = malloc(sizeof(struct ltt_kernel_stream));
280 if (lks == NULL) {
281 perror("kernel stream malloc");
282 goto error;
283 }
284
285 /* Init stream */
286 lks->fd = 0;
287 lks->pathname = NULL;
288 lks->state = 0;
289
290 return lks;
291
292 error:
293 return NULL;
294 }
295
296 void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
297 {
298 DBG("[trace] Closing stream fd %d", stream->fd);
299 /* Close kernel fd */
300 close(stream->fd);
301 free(stream->pathname);
302
303 /* Remove from stream list */
304 cds_list_del(&stream->list);
305 free(stream);
306 }
307
308 void trace_destroy_kernel_event(struct ltt_kernel_event *event)
309 {
310 DBG("[trace] Closing event fd %d", event->fd);
311 /* Close kernel fd */
312 close(event->fd);
313 /* Free attributes */
314 free(event->event);
315
316 /* Remove from event list */
317 cds_list_del(&event->list);
318 free(event);
319 }
320
321 void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
322 {
323 struct ltt_kernel_stream *stream, *stmp;
324 struct ltt_kernel_event *event, *etmp;
325
326 DBG("[trace] Closing channel fd %d", channel->fd);
327 /* Close kernel fd */
328 close(channel->fd);
329 free(channel->pathname);
330 /* Free attributes structure */
331 free(channel->channel);
332
333 /* For each stream in the channel list */
334 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
335 trace_destroy_kernel_stream(stream);
336 }
337
338 /* For each event in the channel list */
339 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
340 trace_destroy_kernel_event(event);
341 }
342
343 /* Remove from channel list */
344 cds_list_del(&channel->list);
345 free(channel);
346 }
347
348 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
349 {
350 DBG("[trace] Closing metadata fd %d", metadata->fd);
351 /* Close kernel fd */
352 close(metadata->fd);
353 /* Free attributes */
354 free(metadata->conf);
355
356 free(metadata);
357 }
358
359 void trace_destroy_kernel_session(struct ltt_kernel_session *session)
360 {
361 struct ltt_kernel_channel *channel, *ctmp;
362
363 DBG("[trace] Closing session fd %d", session->fd);
364 /* Close kernel fds */
365 close(session->fd);
366 if (session->metadata_stream_fd != 0) {
367 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
368 close(session->metadata_stream_fd);
369 }
370
371 if (session->metadata != NULL) {
372 trace_destroy_kernel_metadata(session->metadata);
373 }
374
375 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
376 trace_destroy_kernel_channel(channel);
377 }
378
379 free(session);
380 }
This page took 0.036319 seconds and 4 git commands to generate.