Update lttng kernel modules names
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.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 it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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 "trace-ust.h"
28
29 /*
30 * Find the channel in the hashtable.
31 */
32 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
33 char *name)
34 {
35 struct lttng_ht_node_str *node;
36 struct lttng_ht_iter iter;
37
38 rcu_read_lock();
39 lttng_ht_lookup(ht, (void *)name, &iter);
40 node = lttng_ht_iter_get_node_str(&iter);
41 if (node == NULL) {
42 rcu_read_unlock();
43 goto error;
44 }
45 rcu_read_unlock();
46
47 DBG2("Trace UST channel %s found by name", name);
48
49 return caa_container_of(node, struct ltt_ust_channel, node);
50
51 error:
52 DBG2("Trace UST channel %s not found by name", name);
53 return NULL;
54 }
55
56 /*
57 * Find the event in the hashtable.
58 */
59 struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht,
60 char *name)
61 {
62 struct lttng_ht_node_str *node;
63 struct lttng_ht_iter iter;
64
65 rcu_read_lock();
66 lttng_ht_lookup(ht, (void *) name, &iter);
67 node = lttng_ht_iter_get_node_str(&iter);
68 if (node == NULL) {
69 rcu_read_unlock();
70 goto error;
71 }
72 rcu_read_unlock();
73
74 DBG2("Trace UST event found by name %s", name);
75
76 return caa_container_of(node, struct ltt_ust_event, node);
77
78 error:
79 DBG2("Trace UST event NOT found by name %s", name);
80 return NULL;
81 }
82
83 /*
84 * Allocate and initialize a ust session data structure.
85 *
86 * Return pointer to structure or NULL.
87 */
88 struct ltt_ust_session *trace_ust_create_session(char *path, int session_id,
89 struct lttng_domain *domain)
90 {
91 int ret;
92 struct ltt_ust_session *lus;
93
94 /* Allocate a new ltt ust session */
95 lus = zmalloc(sizeof(struct ltt_ust_session));
96 if (lus == NULL) {
97 PERROR("create ust session zmalloc");
98 goto error;
99 }
100
101 /* Init data structure */
102 lus->id = session_id;
103 lus->start_trace = 0;
104
105 /* Alloc UST domain hash tables */
106 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
107 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
108
109 /* Alloc UST global domain channels' HT */
110 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
111
112 /* Set session path */
113 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
114 if (ret < 0) {
115 PERROR("snprintf kernel traces path");
116 goto error_free_session;
117 }
118
119 DBG2("UST trace session create successful");
120
121 return lus;
122
123 error_free_session:
124 free(lus);
125 error:
126 return NULL;
127 }
128
129 /*
130 * Allocate and initialize a ust channel data structure.
131 *
132 * Return pointer to structure or NULL.
133 */
134 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
135 char *path)
136 {
137 int ret;
138 struct ltt_ust_channel *luc;
139
140 luc = zmalloc(sizeof(struct ltt_ust_channel));
141 if (luc == NULL) {
142 perror("ltt_ust_channel zmalloc");
143 goto error;
144 }
145
146 /* Copy UST channel attributes */
147 luc->attr.overwrite = chan->attr.overwrite;
148 luc->attr.subbuf_size = chan->attr.subbuf_size;
149 luc->attr.num_subbuf = chan->attr.num_subbuf;
150 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
151 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
152 luc->attr.output = chan->attr.output;
153
154 /* Translate to UST output enum */
155 switch (luc->attr.output) {
156 default:
157 luc->attr.output = LTTNG_UST_MMAP;
158 break;
159 }
160
161 /* Copy channel name */
162 strncpy(luc->name, chan->name, sizeof(luc->name));
163 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
164
165 /* Init node */
166 lttng_ht_node_init_str(&luc->node, luc->name);
167 /* Alloc hash tables */
168 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
169 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
170
171 /* Set trace output path */
172 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
173 if (ret < 0) {
174 perror("asprintf ust create channel");
175 goto error_free_channel;
176 }
177
178 DBG2("Trace UST channel %s created", luc->name);
179
180 return luc;
181
182 error_free_channel:
183 free(luc);
184 error:
185 return NULL;
186 }
187
188 /*
189 * Allocate and initialize a ust event. Set name and event type.
190 *
191 * Return pointer to structure or NULL.
192 */
193 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
194 {
195 struct ltt_ust_event *lue;
196
197 lue = zmalloc(sizeof(struct ltt_ust_event));
198 if (lue == NULL) {
199 PERROR("ust event zmalloc");
200 goto error;
201 }
202
203 switch (ev->type) {
204 case LTTNG_EVENT_PROBE:
205 lue->attr.instrumentation = LTTNG_UST_PROBE;
206 break;
207 case LTTNG_EVENT_FUNCTION:
208 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
209 break;
210 case LTTNG_EVENT_FUNCTION_ENTRY:
211 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
212 break;
213 case LTTNG_EVENT_TRACEPOINT:
214 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
215 break;
216 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
217 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT_LOGLEVEL;
218 break;
219 default:
220 ERR("Unknown ust instrumentation type (%d)", ev->type);
221 goto error_free_event;
222 }
223
224 /* Copy event name */
225 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
226 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
227
228 /* Init node */
229 lttng_ht_node_init_str(&lue->node, lue->attr.name);
230 /* Alloc context hash tables */
231 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
232
233 DBG2("Trace UST event %s created", lue->attr.name);
234
235 return lue;
236
237 error_free_event:
238 free(lue);
239 error:
240 return NULL;
241 }
242
243 /*
244 * Allocate and initialize a ust metadata.
245 *
246 * Return pointer to structure or NULL.
247 */
248 struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
249 {
250 int ret;
251 struct ltt_ust_metadata *lum;
252
253 lum = zmalloc(sizeof(struct ltt_ust_metadata));
254 if (lum == NULL) {
255 perror("ust metadata zmalloc");
256 goto error;
257 }
258
259 /* Set default attributes */
260 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
261 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
262 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
263 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
264 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
265 lum->attr.output = LTTNG_UST_MMAP;
266
267 lum->handle = -1;
268 /* Set metadata trace path */
269 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
270 if (ret < 0) {
271 perror("asprintf ust metadata");
272 goto error_free_metadata;
273 }
274
275 return lum;
276
277 error_free_metadata:
278 free(lum);
279 error:
280 return NULL;
281 }
282
283 /*
284 * Allocate and initialize an UST context.
285 *
286 * Return pointer to structure or NULL.
287 */
288 struct ltt_ust_context *trace_ust_create_context(
289 struct lttng_event_context *ctx)
290 {
291 struct ltt_ust_context *uctx;
292
293 uctx = zmalloc(sizeof(struct ltt_ust_context));
294 if (uctx == NULL) {
295 PERROR("zmalloc ltt_ust_context");
296 goto error;
297 }
298
299 uctx->ctx.ctx = ctx->ctx;
300 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
301
302 return uctx;
303
304 error:
305 return NULL;
306 }
307
308 /*
309 * RCU safe free context structure.
310 */
311 static void destroy_context_rcu(struct rcu_head *head)
312 {
313 struct lttng_ht_node_ulong *node =
314 caa_container_of(head, struct lttng_ht_node_ulong, head);
315 struct ltt_ust_context *ctx =
316 caa_container_of(node, struct ltt_ust_context, node);
317
318 free(ctx);
319 }
320
321 /*
322 * Cleanup UST context hash table.
323 */
324 static void destroy_contexts(struct lttng_ht *ht)
325 {
326 int ret;
327 struct lttng_ht_node_ulong *node;
328 struct lttng_ht_iter iter;
329
330 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
331 ret = lttng_ht_del(ht, &iter);
332 if (!ret) {
333 call_rcu(&node->head, destroy_context_rcu);
334 }
335 }
336
337 lttng_ht_destroy(ht);
338 }
339
340 /*
341 * Cleanup ust event structure.
342 */
343 void trace_ust_destroy_event(struct ltt_ust_event *event)
344 {
345 DBG2("Trace destroy UST event %s", event->attr.name);
346 destroy_contexts(event->ctx);
347
348 free(event);
349 }
350
351 /*
352 * URCU intermediate call to complete destroy event.
353 */
354 static void destroy_event_rcu(struct rcu_head *head)
355 {
356 struct lttng_ht_node_str *node =
357 caa_container_of(head, struct lttng_ht_node_str, head);
358 struct ltt_ust_event *event =
359 caa_container_of(node, struct ltt_ust_event, node);
360
361 trace_ust_destroy_event(event);
362 }
363
364 /*
365 * Cleanup UST events hashtable.
366 */
367 static void destroy_events(struct lttng_ht *events)
368 {
369 int ret;
370 struct lttng_ht_node_str *node;
371 struct lttng_ht_iter iter;
372
373 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
374 ret = lttng_ht_del(events, &iter);
375 assert(!ret);
376 call_rcu(&node->head, destroy_event_rcu);
377 }
378
379 lttng_ht_destroy(events);
380 }
381
382 /*
383 * Cleanup ust channel structure.
384 */
385 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
386 {
387 DBG2("Trace destroy UST channel %s", channel->name);
388
389 rcu_read_lock();
390
391 /* Destroying all events of the channel */
392 destroy_events(channel->events);
393 /* Destroying all context of the channel */
394 destroy_contexts(channel->ctx);
395
396 free(channel);
397
398 rcu_read_unlock();
399 }
400
401 /*
402 * URCU intermediate call to complete destroy channel.
403 */
404 static void destroy_channel_rcu(struct rcu_head *head)
405 {
406 struct lttng_ht_node_str *node =
407 caa_container_of(head, struct lttng_ht_node_str, head);
408 struct ltt_ust_channel *channel =
409 caa_container_of(node, struct ltt_ust_channel, node);
410
411 trace_ust_destroy_channel(channel);
412 }
413
414 /*
415 * Cleanup ust metadata structure.
416 */
417 void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
418 {
419 DBG2("Trace UST destroy metadata %d", metadata->handle);
420
421 free(metadata);
422 }
423
424 /*
425 * Iterate over a hash table containing channels and cleanup safely.
426 */
427 static void destroy_channels(struct lttng_ht *channels)
428 {
429 int ret;
430 struct lttng_ht_node_str *node;
431 struct lttng_ht_iter iter;
432
433 rcu_read_lock();
434
435 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
436 ret = lttng_ht_del(channels, &iter);
437 assert(!ret);
438 call_rcu(&node->head, destroy_channel_rcu);
439 }
440
441 lttng_ht_destroy(channels);
442
443 rcu_read_unlock();
444 }
445
446 /*
447 * Cleanup UST pid domain.
448 */
449 static void destroy_domain_pid(struct lttng_ht *ht)
450 {
451 int ret;
452 struct lttng_ht_iter iter;
453 struct ltt_ust_domain_pid *dpid;
454
455 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
456 ret = lttng_ht_del(ht , &iter);
457 assert(!ret);
458 destroy_channels(dpid->channels);
459 }
460
461 lttng_ht_destroy(ht);
462 }
463
464 /*
465 * Cleanup UST exec name domain.
466 */
467 static void destroy_domain_exec(struct lttng_ht *ht)
468 {
469 int ret;
470 struct lttng_ht_iter iter;
471 struct ltt_ust_domain_exec *dexec;
472
473 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
474 ret = lttng_ht_del(ht , &iter);
475 assert(!ret);
476 destroy_channels(dexec->channels);
477 }
478
479 lttng_ht_destroy(ht);
480 }
481
482 /*
483 * Cleanup UST global domain.
484 */
485 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
486 {
487 destroy_channels(dom->channels);
488 }
489
490 /*
491 * Cleanup ust session structure
492 */
493 void trace_ust_destroy_session(struct ltt_ust_session *session)
494 {
495 /* Extra protection */
496 if (session == NULL) {
497 return;
498 }
499
500 rcu_read_lock();
501
502 DBG2("Trace UST destroy session %d", session->id);
503
504 /* Cleaning up UST domain */
505 destroy_domain_global(&session->domain_global);
506 destroy_domain_pid(session->domain_pid);
507 destroy_domain_exec(session->domain_exec);
508
509 free(session);
510
511 rcu_read_unlock();
512 }
This page took 0.040216 seconds and 4 git commands to generate.