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 "../common/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("Trace UST event found by name %s", name
);
75 return caa_container_of(node
, struct ltt_ust_event
, node
);
78 DBG2("Trace UST event NOT found by name %s", name
);
83 * Allocate and initialize a ust session data structure.
85 * Return pointer to structure or NULL.
87 struct ltt_ust_session
*trace_ust_create_session(char *path
, int session_id
,
88 struct lttng_domain
*domain
)
91 struct ltt_ust_session
*lus
;
93 /* Allocate a new ltt ust session */
94 lus
= zmalloc(sizeof(struct ltt_ust_session
));
96 PERROR("create ust session zmalloc");
100 /* Init data structure */
101 lus
->id
= session_id
;
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");
115 goto error_free_session
;
118 DBG2("UST trace session create successful");
129 * Allocate and initialize a ust channel data structure.
131 * Return pointer to structure or NULL.
133 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
137 struct ltt_ust_channel
*luc
;
139 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
141 perror("ltt_ust_channel zmalloc");
145 /* Copy UST channel attributes */
146 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
147 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
148 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
149 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
150 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
151 luc
->attr
.output
= chan
->attr
.output
;
153 /* Translate to UST output enum */
154 switch (luc
->attr
.output
) {
156 luc
->attr
.output
= LTTNG_UST_MMAP
;
160 /* Copy channel name */
161 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
162 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
165 hashtable_node_init(&luc
->node
, (void *) luc
->name
, strlen(luc
->name
));
166 /* Alloc hash tables */
167 luc
->events
= hashtable_new_str(0);
168 luc
->ctx
= hashtable_new(0);
170 /* Set trace output path */
171 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
173 perror("asprintf ust create channel");
174 goto error_free_channel
;
177 DBG2("Trace UST channel %s created", luc
->name
);
188 * Allocate and initialize a ust event. Set name and event type.
190 * Return pointer to structure or NULL.
192 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
194 struct ltt_ust_event
*lue
;
196 lue
= zmalloc(sizeof(struct ltt_ust_event
));
198 PERROR("ust event zmalloc");
203 case LTTNG_EVENT_PROBE
:
204 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
206 case LTTNG_EVENT_FUNCTION
:
207 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
209 case LTTNG_EVENT_FUNCTION_ENTRY
:
210 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
212 case LTTNG_EVENT_TRACEPOINT
:
213 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
215 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL
:
216 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT_LOGLEVEL
;
219 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
220 goto error_free_event
;
223 /* Copy event name */
224 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
225 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
228 hashtable_node_init(&lue
->node
, (void *) lue
->attr
.name
,
229 strlen(lue
->attr
.name
));
230 /* Alloc context hash tables */
231 lue
->ctx
= hashtable_new(0);
233 DBG2("Trace UST event %s created", lue
->attr
.name
);
244 * Allocate and initialize a ust metadata.
246 * Return pointer to structure or NULL.
248 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
251 struct ltt_ust_metadata
*lum
;
253 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
255 perror("ust metadata zmalloc");
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
;
268 /* Set metadata trace path */
269 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
271 perror("asprintf ust metadata");
272 goto error_free_metadata
;
284 * Allocate and initialize an UST context.
286 * Return pointer to structure or NULL.
288 struct ltt_ust_context
*trace_ust_create_context(
289 struct lttng_event_context
*ctx
)
291 struct ltt_ust_context
*uctx
;
293 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
295 PERROR("zmalloc ltt_ust_context");
299 uctx
->ctx
.ctx
= ctx
->ctx
;
300 hashtable_node_init(&uctx
->node
, (void *)((unsigned long) uctx
->ctx
.ctx
),
310 * RCU safe free context structure.
312 static void destroy_context_rcu(struct rcu_head
*head
)
314 struct cds_lfht_node
*node
=
315 caa_container_of(head
, struct cds_lfht_node
, head
);
316 struct ltt_ust_context
*ctx
=
317 caa_container_of(node
, struct ltt_ust_context
, node
);
323 * Cleanup UST context hash table.
325 static void destroy_context(struct cds_lfht
*ht
)
328 struct cds_lfht_node
*node
;
329 struct cds_lfht_iter iter
;
331 cds_lfht_for_each(ht
, &iter
, node
) {
332 ret
= hashtable_del(ht
, &iter
);
334 call_rcu(&node
->head
, destroy_context_rcu
);
338 hashtable_destroy(ht
);
342 * Cleanup ust event structure.
344 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
346 DBG2("Trace destroy UST event %s", event
->attr
.name
);
347 destroy_context(event
->ctx
);
353 * URCU intermediate call to complete destroy event.
355 static void destroy_event_rcu(struct rcu_head
*head
)
357 struct cds_lfht_node
*node
=
358 caa_container_of(head
, struct cds_lfht_node
, head
);
359 struct ltt_ust_event
*event
=
360 caa_container_of(node
, struct ltt_ust_event
, node
);
362 trace_ust_destroy_event(event
);
366 * Cleanup UST events hashtable.
368 static void destroy_event(struct cds_lfht
*events
)
371 struct cds_lfht_node
*node
;
372 struct cds_lfht_iter iter
;
374 cds_lfht_for_each(events
, &iter
, node
) {
375 ret
= hashtable_del(events
, &iter
);
377 call_rcu(&node
->head
, destroy_event_rcu
);
381 hashtable_destroy(events
);
385 * Cleanup ust channel structure.
387 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
390 struct cds_lfht_node
*node
;
391 struct cds_lfht_iter iter
;
393 DBG2("Trace destroy UST channel %s", channel
->name
);
397 cds_lfht_for_each(channel
->events
, &iter
, node
) {
398 ret
= hashtable_del(channel
->events
, &iter
);
400 destroy_event(channel
->events
);
404 destroy_context(channel
->ctx
);
411 * URCU intermediate call to complete destroy channel.
413 static void destroy_channel_rcu(struct rcu_head
*head
)
415 struct cds_lfht_node
*node
=
416 caa_container_of(head
, struct cds_lfht_node
, head
);
417 struct ltt_ust_channel
*channel
=
418 caa_container_of(node
, struct ltt_ust_channel
, node
);
420 trace_ust_destroy_channel(channel
);
424 * Cleanup ust metadata structure.
426 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
428 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
434 * Iterate over a hash table containing channels and cleanup safely.
436 static void destroy_channels(struct cds_lfht
*channels
)
439 struct cds_lfht_node
*node
;
440 struct cds_lfht_iter iter
;
442 cds_lfht_for_each(channels
, &iter
, node
) {
443 ret
= hashtable_del(channels
, &iter
);
445 call_rcu(&node
->head
, destroy_channel_rcu
);
449 hashtable_destroy(channels
);
453 * Cleanup UST pid domain.
455 static void destroy_domain_pid(struct cds_lfht
*ht
)
458 struct cds_lfht_iter iter
;
459 struct ltt_ust_domain_pid
*dpid
;
461 cds_lfht_for_each_entry(ht
, &iter
, dpid
, node
) {
462 ret
= hashtable_del(ht
, &iter
);
464 destroy_channels(dpid
->channels
);
468 hashtable_destroy(ht
);
472 * Cleanup UST exec name domain.
474 static void destroy_domain_exec(struct cds_lfht
*ht
)
477 struct cds_lfht_iter iter
;
478 struct ltt_ust_domain_exec
*dexec
;
480 cds_lfht_for_each_entry(ht
, &iter
, dexec
, node
) {
481 ret
= hashtable_del(ht
, &iter
);
483 destroy_channels(dexec
->channels
);
487 hashtable_destroy(ht
);
491 * Cleanup UST global domain.
493 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
495 destroy_channels(dom
->channels
);
499 * Cleanup ust session structure
501 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
503 /* Extra protection */
504 if (session
== NULL
) {
510 DBG2("Trace UST destroy session %d", session
->id
);
512 /* Cleaning up UST domain */
513 destroy_domain_global(&session
->domain_global
);
514 destroy_domain_pid(session
->domain_pid
);
515 destroy_domain_exec(session
->domain_exec
);