2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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
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.
25 #include <lttng-share.h>
27 #include "hashtable.h"
28 #include "trace-ust.h"
31 * Find the channel in the hashtable.
33 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct cds_lfht
*ht
,
36 struct cds_lfht_node
*node
;
37 struct cds_lfht_iter iter
;
40 node
= hashtable_lookup(ht
, (void *) name
, strlen(name
), &iter
);
47 DBG2("Trace UST channel %s found by name", name
);
49 return caa_container_of(node
, struct ltt_ust_channel
, node
);
52 DBG2("Trace UST channel %s not found by name", name
);
57 * Find the event in the hashtable.
59 struct ltt_ust_event
*trace_ust_find_event_by_name(struct cds_lfht
*ht
,
62 struct cds_lfht_node
*node
;
63 struct cds_lfht_iter iter
;
66 node
= hashtable_lookup(ht
, (void *) name
, strlen(name
), &iter
);
73 DBG2("Found UST event by name %s", name
);
75 return caa_container_of(node
, struct ltt_ust_event
, node
);
82 * Allocate and initialize a ust session data structure.
84 * Return pointer to structure or NULL.
86 struct ltt_ust_session
*trace_ust_create_session(char *path
, unsigned int uid
,
87 struct lttng_domain
*domain
)
90 struct ltt_ust_session
*lus
;
92 /* Allocate a new ltt ust session */
93 lus
= malloc(sizeof(struct ltt_ust_session
));
95 PERROR("create ust session malloc");
99 /* Init data structure */
100 lus
->consumer_fds_sent
= 0;
102 lus
->start_trace
= 0;
104 /* Alloc UST domain hash tables */
105 lus
->domain_pid
= hashtable_new(0);
106 lus
->domain_exec
= hashtable_new_str(0);
108 /* Alloc UST global domain channels' HT */
109 lus
->domain_global
.channels
= hashtable_new_str(0);
111 /* Set session path */
112 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
114 PERROR("snprintf kernel traces path");
118 DBG2("UST trace session create successful");
127 * Allocate and initialize a ust channel data structure.
129 * Return pointer to structure or NULL.
131 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
135 struct ltt_ust_channel
*luc
;
137 luc
= malloc(sizeof(struct ltt_ust_channel
));
139 perror("ltt_ust_channel malloc");
143 /* Copy UST channel attributes */
144 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
145 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
146 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
147 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
148 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
149 luc
->attr
.output
= chan
->attr
.output
;
151 /* Translate to UST output enum */
152 switch (luc
->attr
.output
) {
154 luc
->attr
.output
= LTTNG_UST_MMAP
;
158 /* Copy channel name */
159 strncpy(luc
->name
, chan
->name
, sizeof(&luc
->name
));
160 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
163 hashtable_node_init(&luc
->node
, (void *) luc
->name
, strlen(luc
->name
));
164 /* Alloc hash tables */
165 luc
->events
= hashtable_new_str(0);
166 luc
->ctx
= hashtable_new_str(0);
168 /* Set trace output path */
169 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
171 perror("asprintf ust create channel");
175 DBG2("Trace UST channel %s created", luc
->name
);
184 * Allocate and initialize a ust event. Set name and event type.
186 * Return pointer to structure or NULL.
188 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
190 struct ltt_ust_event
*lue
;
192 lue
= malloc(sizeof(struct ltt_ust_event
));
194 PERROR("ust event malloc");
199 case LTTNG_EVENT_PROBE
:
200 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
202 case LTTNG_EVENT_FUNCTION
:
203 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
205 case LTTNG_EVENT_FUNCTION_ENTRY
:
206 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
208 case LTTNG_EVENT_TRACEPOINT
:
209 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
212 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
216 /* Copy event name */
217 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
218 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
221 hashtable_node_init(&lue
->node
, (void *) lue
->attr
.name
,
222 strlen(lue
->attr
.name
));
223 /* Alloc context hash tables */
224 lue
->ctx
= hashtable_new_str(5);
233 * Allocate and initialize a ust metadata.
235 * Return pointer to structure or NULL.
237 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
240 struct ltt_ust_metadata
*lum
;
242 lum
= malloc(sizeof(struct ltt_ust_metadata
));
244 perror("ust metadata malloc");
248 /* Set default attributes */
249 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
250 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
251 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
252 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
253 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
254 lum
->attr
.output
= LTTNG_UST_MMAP
;
257 /* Set metadata trace path */
258 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
260 perror("asprintf ust metadata");
271 * RCU safe free context structure.
273 static void destroy_context_rcu(struct rcu_head
*head
)
275 struct cds_lfht_node
*node
=
276 caa_container_of(head
, struct cds_lfht_node
, head
);
277 struct ltt_ust_context
*ctx
=
278 caa_container_of(node
, struct ltt_ust_context
, node
);
284 * Cleanup UST context hash table.
286 static void destroy_context(struct cds_lfht
*ht
)
289 struct cds_lfht_node
*node
;
290 struct cds_lfht_iter iter
;
292 hashtable_get_first(ht
, &iter
);
293 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
294 ret
= hashtable_del(ht
, &iter
);
296 call_rcu(&node
->head
, destroy_context_rcu
);
298 hashtable_get_next(ht
, &iter
);
303 * Cleanup ust event structure.
305 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
307 DBG2("Trace destroy UST event %s", event
->attr
.name
);
308 destroy_context(event
->ctx
);
313 * URCU intermediate call to complete destroy event.
315 static void destroy_event_rcu(struct rcu_head
*head
)
317 struct cds_lfht_node
*node
=
318 caa_container_of(head
, struct cds_lfht_node
, head
);
319 struct ltt_ust_event
*event
=
320 caa_container_of(node
, struct ltt_ust_event
, node
);
322 trace_ust_destroy_event(event
);
326 * Cleanup ust channel structure.
328 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
331 struct cds_lfht_node
*node
;
332 struct cds_lfht_iter iter
;
334 DBG2("Trace destroy UST channel %s", channel
->name
);
338 hashtable_get_first(channel
->events
, &iter
);
339 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
340 ret
= hashtable_del(channel
->events
, &iter
);
342 call_rcu(&node
->head
, destroy_event_rcu
);
344 hashtable_get_next(channel
->events
, &iter
);
347 free(channel
->events
);
348 destroy_context(channel
->ctx
);
355 * URCU intermediate call to complete destroy channel.
357 static void destroy_channel_rcu(struct rcu_head
*head
)
359 struct cds_lfht_node
*node
=
360 caa_container_of(head
, struct cds_lfht_node
, head
);
361 struct ltt_ust_channel
*channel
=
362 caa_container_of(node
, struct ltt_ust_channel
, node
);
364 trace_ust_destroy_channel(channel
);
368 * Cleanup ust metadata structure.
370 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
372 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
378 * Iterate over a hash table containing channels and cleanup safely.
380 static void destroy_channels(struct cds_lfht
*channels
)
383 struct cds_lfht_node
*node
;
384 struct cds_lfht_iter iter
;
386 hashtable_get_first(channels
, &iter
);
387 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
388 ret
= hashtable_del(channels
, &iter
);
390 call_rcu(&node
->head
, destroy_channel_rcu
);
392 hashtable_get_next(channels
, &iter
);
397 * Cleanup UST pid domain.
399 static void destroy_domain_pid(struct cds_lfht
*ht
)
402 struct cds_lfht_node
*node
;
403 struct cds_lfht_iter iter
;
404 struct ltt_ust_domain_pid
*d
;
406 hashtable_get_first(ht
, &iter
);
407 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
408 ret
= hashtable_del(ht
, &iter
);
410 d
= caa_container_of(node
, struct ltt_ust_domain_pid
, node
);
411 destroy_channels(d
->channels
);
413 hashtable_get_next(ht
, &iter
);
418 * Cleanup UST exec name domain.
420 static void destroy_domain_exec(struct cds_lfht
*ht
)
423 struct cds_lfht_node
*node
;
424 struct cds_lfht_iter iter
;
425 struct ltt_ust_domain_exec
*d
;
427 hashtable_get_first(ht
, &iter
);
428 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
429 ret
= hashtable_del(ht
, &iter
);
431 d
= caa_container_of(node
, struct ltt_ust_domain_exec
, node
);
432 destroy_channels(d
->channels
);
434 hashtable_get_next(ht
, &iter
);
439 * Cleanup UST global domain.
441 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
443 destroy_channels(dom
->channels
);
447 * Cleanup ust session structure
449 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
452 if (session
== NULL
) {
458 DBG2("Trace UST destroy session %d", session
->uid
);
460 //if (session->metadata != NULL) {
461 // trace_ust_destroy_metadata(session->metadata);
464 /* Cleaning up UST domain */
465 destroy_domain_global(&session
->domain_global
);
466 destroy_domain_pid(session
->domain_pid
);
467 destroy_domain_exec(session
->domain_exec
);