Preliminary work for full UST support
[lttng-tools.git] / ltt-sessiond / trace-ust.c
CommitLineData
97ee3a89
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 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 <lttngerr.h>
25#include <lttng-share.h>
26
f6a9efaa 27#include "hashtable.h"
97ee3a89
DG
28#include "trace-ust.h"
29
0177d773 30/*
f6a9efaa 31 * Find the channel in the hashtable.
0177d773 32 */
f6a9efaa
DG
33struct ltt_ust_channel *trace_ust_find_channel_by_name(struct cds_lfht *ht,
34 char *name)
0177d773 35{
f6a9efaa
DG
36 struct cds_lfht_node *node;
37 struct cds_lfht_iter iter;
0177d773 38
f6a9efaa
DG
39 rcu_read_lock();
40 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
41 if (node == NULL) {
42 rcu_read_unlock();
44d3bd01
DG
43 goto error;
44 }
f6a9efaa 45 rcu_read_unlock();
44d3bd01 46
f6a9efaa 47 DBG2("Trace UST channel %s found by name", name);
0177d773 48
f6a9efaa 49 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
50
51error:
f6a9efaa 52 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
53 return NULL;
54}
55
56/*
f6a9efaa 57 * Find the event in the hashtable.
97ee3a89 58 */
f6a9efaa
DG
59struct ltt_ust_event *trace_ust_find_event_by_name(struct cds_lfht *ht,
60 char *name)
97ee3a89 61{
f6a9efaa
DG
62 struct cds_lfht_node *node;
63 struct cds_lfht_iter iter;
97ee3a89 64
f6a9efaa
DG
65 rcu_read_lock();
66 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
67 if (node == NULL) {
68 rcu_read_unlock();
97ee3a89
DG
69 goto error;
70 }
f6a9efaa 71 rcu_read_unlock();
97ee3a89 72
f6a9efaa
DG
73 DBG2("Found UST event by name %s", name);
74
75 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
76
77error:
78 return NULL;
79}
80
81/*
82 * Allocate and initialize a ust session data structure.
83 *
84 * Return pointer to structure or NULL.
85 */
48842b30 86struct ltt_ust_session *trace_ust_create_session(char *path, unsigned int uid,
44d3bd01 87 struct lttng_domain *domain)
97ee3a89 88{
0177d773 89 int ret;
97ee3a89
DG
90 struct ltt_ust_session *lus;
91
92 /* Allocate a new ltt ust session */
93 lus = malloc(sizeof(struct ltt_ust_session));
94 if (lus == NULL) {
95 perror("create ust session malloc");
96 goto error;
97 }
98
99 /* Init data structure */
3bd1e081 100 lus->consumer_fds_sent = 0;
48842b30 101 lus->uid = uid;
97ee3a89 102
f6a9efaa
DG
103 /* Alloc UST domain hash tables */
104 lus->domain_pid = hashtable_new(0);
105 lus->domain_exec = hashtable_new_str(0);
106
107 /* Alloc UST global domain channels' HT */
108 lus->domain_global.channels = hashtable_new_str(0);
44d3bd01 109
0177d773 110 /* Set session path */
48842b30 111 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
0177d773 112 if (ret < 0) {
44d3bd01 113 PERROR("snprintf kernel traces path");
0177d773
DG
114 goto error;
115 }
116
44d3bd01
DG
117 DBG2("UST trace session create successful");
118
97ee3a89
DG
119 return lus;
120
121error:
122 return NULL;
123}
124
125/*
126 * Allocate and initialize a ust channel data structure.
127 *
128 * Return pointer to structure or NULL.
129 */
44d3bd01
DG
130struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
131 char *path)
97ee3a89
DG
132{
133 int ret;
134 struct ltt_ust_channel *luc;
135
136 luc = malloc(sizeof(struct ltt_ust_channel));
137 if (luc == NULL) {
138 perror("ltt_ust_channel malloc");
139 goto error;
140 }
141
44d3bd01 142 /* Copy UST channel attributes */
48842b30
DG
143 luc->attr.overwrite = chan->attr.overwrite;
144 luc->attr.subbuf_size = chan->attr.subbuf_size;
145 luc->attr.num_subbuf = chan->attr.num_subbuf;
146 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
147 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
148 luc->attr.output = chan->attr.output;
44d3bd01
DG
149
150 /* Translate to UST output enum */
151 switch (luc->attr.output) {
152 default:
153 luc->attr.output = LTTNG_UST_MMAP;
154 break;
97ee3a89 155 }
97ee3a89 156
97ee3a89 157 /* Copy channel name */
44d3bd01 158 strncpy(luc->name, chan->name, sizeof(&luc->name));
97ee3a89
DG
159 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
160
f6a9efaa
DG
161 /* Init node */
162 hashtable_node_init(&luc->node, (void *) luc->name, strlen(luc->name));
163 /* Alloc hash tables */
164 luc->events = hashtable_new_str(0);
165 luc->ctx = hashtable_new_str(0);
166
97ee3a89 167 /* Set trace output path */
48842b30 168 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89
DG
169 if (ret < 0) {
170 perror("asprintf ust create channel");
171 goto error;
172 }
173
f6a9efaa
DG
174 DBG2("Trace UST channel %s created", luc->name);
175
97ee3a89
DG
176 return luc;
177
178error:
179 return NULL;
180}
181
182/*
183 * Allocate and initialize a ust event. Set name and event type.
184 *
185 * Return pointer to structure or NULL.
186 */
187struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
188{
189 struct ltt_ust_event *lue;
97ee3a89
DG
190
191 lue = malloc(sizeof(struct ltt_ust_event));
44d3bd01
DG
192 if (lue == NULL) {
193 PERROR("ust event malloc");
97ee3a89
DG
194 goto error;
195 }
196
197 switch (ev->type) {
198 case LTTNG_EVENT_PROBE:
44d3bd01 199 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
200 break;
201 case LTTNG_EVENT_FUNCTION:
44d3bd01 202 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
203 break;
204 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 205 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
206 break;
207 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 208 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
209 break;
210 default:
211 ERR("Unknown ust instrumentation type (%d)", ev->type);
212 goto error;
213 }
214
215 /* Copy event name */
44d3bd01
DG
216 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
217 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 218
f6a9efaa
DG
219 /* Init node */
220 hashtable_node_init(&lue->node, (void *) lue->attr.name,
221 strlen(lue->attr.name));
222 /* Alloc context hash tables */
223 lue->ctx = hashtable_new_str(5);
97ee3a89
DG
224
225 return lue;
226
227error:
228 return NULL;
229}
230
231/*
232 * Allocate and initialize a ust metadata.
233 *
234 * Return pointer to structure or NULL.
235 */
236struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
237{
238 int ret;
239 struct ltt_ust_metadata *lum;
97ee3a89
DG
240
241 lum = malloc(sizeof(struct ltt_ust_metadata));
44d3bd01 242 if (lum == NULL) {
97ee3a89
DG
243 perror("ust metadata malloc");
244 goto error;
245 }
246
247 /* Set default attributes */
44d3bd01
DG
248 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
249 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
250 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
251 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
252 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
253 lum->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
254
97ee3a89
DG
255 lum->handle = -1;
256 /* Set metadata trace path */
f6a9efaa 257 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89
DG
258 if (ret < 0) {
259 perror("asprintf ust metadata");
260 goto error;
261 }
262
263 return lum;
264
265error:
266 return NULL;
267}
268
f6a9efaa
DG
269/*
270 * RCU safe free context structure.
271 */
272static void destroy_context_rcu(struct rcu_head *head)
273{
274 struct cds_lfht_node *node =
275 caa_container_of(head, struct cds_lfht_node, head);
276 struct ltt_ust_context *ctx =
277 caa_container_of(node, struct ltt_ust_context, node);
278
279 free(ctx);
280}
281
282/*
283 * Cleanup UST context hash table.
284 */
285static void destroy_context(struct cds_lfht *ht)
286{
287 int ret;
288 struct cds_lfht_node *node;
289 struct cds_lfht_iter iter;
290
291 hashtable_get_first(ht, &iter);
292 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
293 ret = hashtable_del(ht, &iter);
294 if (!ret) {
295 call_rcu(&node->head, destroy_context_rcu);
296 }
297 hashtable_get_next(ht, &iter);
298 }
299}
300
97ee3a89
DG
301/*
302 * Cleanup ust event structure.
303 */
304void trace_ust_destroy_event(struct ltt_ust_event *event)
305{
f6a9efaa
DG
306 DBG2("Trace destroy UST event %s", event->attr.name);
307 destroy_context(event->ctx);
97ee3a89
DG
308 free(event);
309}
310
f6a9efaa
DG
311/*
312 * URCU intermediate call to complete destroy event.
313 */
314static void destroy_event_rcu(struct rcu_head *head)
315{
316 struct cds_lfht_node *node =
317 caa_container_of(head, struct cds_lfht_node, head);
318 struct ltt_ust_event *event =
319 caa_container_of(node, struct ltt_ust_event, node);
320
321 trace_ust_destroy_event(event);
322}
323
97ee3a89
DG
324/*
325 * Cleanup ust channel structure.
326 */
327void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
328{
f6a9efaa
DG
329 int ret;
330 struct cds_lfht_node *node;
331 struct cds_lfht_iter iter;
97ee3a89 332
f6a9efaa 333 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 334
f6a9efaa
DG
335 rcu_read_lock();
336
337 hashtable_get_first(channel->events, &iter);
338 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
339 ret = hashtable_del(channel->events, &iter);
340 if (!ret) {
341 call_rcu(&node->head, destroy_event_rcu);
342 }
343 hashtable_get_next(channel->events, &iter);
97ee3a89
DG
344 }
345
f6a9efaa
DG
346 free(channel->events);
347 destroy_context(channel->ctx);
97ee3a89 348 free(channel);
f6a9efaa
DG
349
350 rcu_read_unlock();
351}
352
353/*
354 * URCU intermediate call to complete destroy channel.
355 */
356static void destroy_channel_rcu(struct rcu_head *head)
357{
358 struct cds_lfht_node *node =
359 caa_container_of(head, struct cds_lfht_node, head);
360 struct ltt_ust_channel *channel =
361 caa_container_of(node, struct ltt_ust_channel, node);
362
363 trace_ust_destroy_channel(channel);
97ee3a89
DG
364}
365
366/*
367 * Cleanup ust metadata structure.
368 */
369void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
370{
f6a9efaa 371 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89 372
97ee3a89
DG
373 free(metadata);
374}
375
376/*
f6a9efaa 377 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 378 */
f6a9efaa
DG
379static void destroy_channels(struct cds_lfht *channels)
380{
381 int ret;
382 struct cds_lfht_node *node;
383 struct cds_lfht_iter iter;
384
385 hashtable_get_first(channels, &iter);
386 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
387 ret = hashtable_del(channels, &iter);
388 if (!ret) {
389 call_rcu(&node->head, destroy_channel_rcu);
390 }
391 hashtable_get_next(channels, &iter);
392 }
393}
394
395/*
396 * Cleanup UST pid domain.
397 */
398static void destroy_domain_pid(struct cds_lfht *ht)
399{
400 int ret;
401 struct cds_lfht_node *node;
402 struct cds_lfht_iter iter;
403 struct ltt_ust_domain_pid *d;
404
405 hashtable_get_first(ht, &iter);
406 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
407 ret = hashtable_del(ht , &iter);
408 if (!ret) {
409 d = caa_container_of(node, struct ltt_ust_domain_pid, node);
410 destroy_channels(d->channels);
411 }
412 hashtable_get_next(ht, &iter);
413 }
414}
415
416/*
417 * Cleanup UST exec name domain.
418 */
419static void destroy_domain_exec(struct cds_lfht *ht)
97ee3a89 420{
f6a9efaa
DG
421 int ret;
422 struct cds_lfht_node *node;
423 struct cds_lfht_iter iter;
424 struct ltt_ust_domain_exec *d;
425
426 hashtable_get_first(ht, &iter);
427 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
428 ret = hashtable_del(ht , &iter);
429 if (!ret) {
430 d = caa_container_of(node, struct ltt_ust_domain_exec, node);
431 destroy_channels(d->channels);
432 }
433 hashtable_get_next(ht, &iter);
434 }
435}
97ee3a89 436
f6a9efaa
DG
437/*
438 * Cleanup UST global domain.
439 */
440static void destroy_domain_global(struct ltt_ust_domain_global *dom)
441{
442 destroy_channels(dom->channels);
443}
97ee3a89 444
f6a9efaa
DG
445/*
446 * Cleanup ust session structure
447 */
448void trace_ust_destroy_session(struct ltt_ust_session *session)
449{
97ee3a89
DG
450 /* Extra safety */
451 if (session == NULL) {
452 return;
453 }
454
f6a9efaa
DG
455 rcu_read_lock();
456
48842b30 457 DBG2("Trace UST destroy session %d", session->uid);
f6a9efaa 458
48842b30
DG
459 //if (session->metadata != NULL) {
460 // trace_ust_destroy_metadata(session->metadata);
461 //}
97ee3a89 462
f6a9efaa
DG
463 /* Cleaning up UST domain */
464 destroy_domain_global(&session->domain_global);
465 destroy_domain_pid(session->domain_pid);
466 destroy_domain_exec(session->domain_exec);
44d3bd01 467
97ee3a89 468 free(session);
f6a9efaa
DG
469
470 rcu_read_unlock();
97ee3a89 471}
This page took 0.068607 seconds and 4 git commands to generate.