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.
26 #include <lttng-share.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 lttng_ht
*ht
,
36 struct lttng_ht_node_str
*node
;
37 struct lttng_ht_iter iter
;
40 lttng_ht_lookup(ht
, (void *)name
, &iter
);
41 node
= lttng_ht_iter_get_node_str(&iter
);
48 DBG2("Trace UST channel %s found by name", name
);
50 return caa_container_of(node
, struct ltt_ust_channel
, node
);
53 DBG2("Trace UST channel %s not found by name", name
);
58 * Find the event in the hashtable.
60 struct ltt_ust_event
*trace_ust_find_event_by_name(struct lttng_ht
*ht
,
63 struct lttng_ht_node_str
*node
;
64 struct lttng_ht_iter iter
;
67 lttng_ht_lookup(ht
, (void *) name
, &iter
);
68 node
= lttng_ht_iter_get_node_str(&iter
);
75 DBG2("Trace UST event found by name %s", name
);
77 return caa_container_of(node
, struct ltt_ust_event
, node
);
80 DBG2("Trace UST event NOT found by name %s", name
);
85 * Allocate and initialize a ust session data structure.
87 * Return pointer to structure or NULL.
89 struct ltt_ust_session
*trace_ust_create_session(char *path
, int session_id
,
90 struct lttng_domain
*domain
)
93 struct ltt_ust_session
*lus
;
95 /* Allocate a new ltt ust session */
96 lus
= zmalloc(sizeof(struct ltt_ust_session
));
98 PERROR("create ust session zmalloc");
102 /* Init data structure */
103 lus
->id
= session_id
;
104 lus
->start_trace
= 0;
106 /* Alloc UST domain hash tables */
107 lus
->domain_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
108 lus
->domain_exec
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
110 /* Alloc UST global domain channels' HT */
111 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
113 /* Set session path */
114 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
116 PERROR("snprintf kernel traces path");
117 goto error_free_session
;
120 DBG2("UST trace session create successful");
131 * Allocate and initialize a ust channel data structure.
133 * Return pointer to structure or NULL.
135 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
139 struct ltt_ust_channel
*luc
;
141 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
143 perror("ltt_ust_channel zmalloc");
147 /* Copy UST channel attributes */
148 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
149 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
150 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
151 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
152 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
153 luc
->attr
.output
= chan
->attr
.output
;
155 /* Translate to UST output enum */
156 switch (luc
->attr
.output
) {
158 luc
->attr
.output
= LTTNG_UST_MMAP
;
162 /* Copy channel name */
163 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
164 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
167 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
168 /* Alloc hash tables */
169 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
170 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
172 /* Set trace output path */
173 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
175 perror("asprintf ust create channel");
176 goto error_free_channel
;
179 DBG2("Trace UST channel %s created", luc
->name
);
190 * Allocate and initialize a ust event. Set name and event type.
192 * Return pointer to structure or NULL.
194 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
196 struct ltt_ust_event
*lue
;
198 lue
= zmalloc(sizeof(struct ltt_ust_event
));
200 PERROR("ust event zmalloc");
205 case LTTNG_EVENT_PROBE
:
206 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
208 case LTTNG_EVENT_FUNCTION
:
209 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
211 case LTTNG_EVENT_FUNCTION_ENTRY
:
212 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
214 case LTTNG_EVENT_TRACEPOINT
:
215 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
217 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL
:
218 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT_LOGLEVEL
;
221 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
222 goto error_free_event
;
225 /* Copy event name */
226 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
227 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
230 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
231 /* Alloc context hash tables */
232 lue
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
234 DBG2("Trace UST event %s created", lue
->attr
.name
);
245 * Allocate and initialize a ust metadata.
247 * Return pointer to structure or NULL.
249 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
252 struct ltt_ust_metadata
*lum
;
254 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
256 perror("ust metadata zmalloc");
260 /* Set default attributes */
261 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
262 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
263 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
264 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
265 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
266 lum
->attr
.output
= LTTNG_UST_MMAP
;
269 /* Set metadata trace path */
270 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
272 perror("asprintf ust metadata");
273 goto error_free_metadata
;
285 * Allocate and initialize an UST context.
287 * Return pointer to structure or NULL.
289 struct ltt_ust_context
*trace_ust_create_context(
290 struct lttng_event_context
*ctx
)
292 struct ltt_ust_context
*uctx
;
294 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
296 PERROR("zmalloc ltt_ust_context");
300 uctx
->ctx
.ctx
= ctx
->ctx
;
301 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
310 * RCU safe free context structure.
312 static void destroy_context_rcu(struct rcu_head
*head
)
314 struct lttng_ht_node_ulong
*node
=
315 caa_container_of(head
, struct lttng_ht_node_ulong
, 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_contexts(struct lttng_ht
*ht
)
328 struct lttng_ht_node_ulong
*node
;
329 struct lttng_ht_iter iter
;
331 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
332 ret
= lttng_ht_del(ht
, &iter
);
334 call_rcu(&node
->head
, destroy_context_rcu
);
338 lttng_ht_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_contexts(event
->ctx
);
353 * URCU intermediate call to complete destroy event.
355 static void destroy_event_rcu(struct rcu_head
*head
)
357 struct lttng_ht_node_str
*node
=
358 caa_container_of(head
, struct lttng_ht_node_str
, 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_events(struct lttng_ht
*events
)
371 struct lttng_ht_node_str
*node
;
372 struct lttng_ht_iter iter
;
374 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
375 ret
= lttng_ht_del(events
, &iter
);
377 call_rcu(&node
->head
, destroy_event_rcu
);
380 lttng_ht_destroy(events
);
384 * Cleanup ust channel structure.
386 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
388 DBG2("Trace destroy UST channel %s", channel
->name
);
392 /* Destroying all events of the channel */
393 destroy_events(channel
->events
);
394 /* Destroying all context of the channel */
395 destroy_contexts(channel
->ctx
);
403 * URCU intermediate call to complete destroy channel.
405 static void destroy_channel_rcu(struct rcu_head
*head
)
407 struct lttng_ht_node_str
*node
=
408 caa_container_of(head
, struct lttng_ht_node_str
, head
);
409 struct ltt_ust_channel
*channel
=
410 caa_container_of(node
, struct ltt_ust_channel
, node
);
412 trace_ust_destroy_channel(channel
);
416 * Cleanup ust metadata structure.
418 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
420 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
426 * Iterate over a hash table containing channels and cleanup safely.
428 static void destroy_channels(struct lttng_ht
*channels
)
431 struct lttng_ht_node_str
*node
;
432 struct lttng_ht_iter iter
;
436 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
437 ret
= lttng_ht_del(channels
, &iter
);
439 call_rcu(&node
->head
, destroy_channel_rcu
);
442 lttng_ht_destroy(channels
);
448 * Cleanup UST pid domain.
450 static void destroy_domain_pid(struct lttng_ht
*ht
)
453 struct lttng_ht_iter iter
;
454 struct ltt_ust_domain_pid
*dpid
;
456 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
457 ret
= lttng_ht_del(ht
, &iter
);
459 destroy_channels(dpid
->channels
);
462 lttng_ht_destroy(ht
);
466 * Cleanup UST exec name domain.
468 static void destroy_domain_exec(struct lttng_ht
*ht
)
471 struct lttng_ht_iter iter
;
472 struct ltt_ust_domain_exec
*dexec
;
474 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
475 ret
= lttng_ht_del(ht
, &iter
);
477 destroy_channels(dexec
->channels
);
480 lttng_ht_destroy(ht
);
484 * Cleanup UST global domain.
486 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
488 destroy_channels(dom
->channels
);
492 * Cleanup ust session structure
494 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
496 /* Extra protection */
497 if (session
== NULL
) {
503 DBG2("Trace UST destroy session %d", session
->id
);
505 /* Cleaning up UST domain */
506 destroy_domain_global(&session
->domain_global
);
507 destroy_domain_pid(session
->domain_pid
);
508 destroy_domain_exec(session
->domain_exec
);