2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/common.h>
25 #include <common/defaults.h>
27 #include "trace-ust.h"
30 * Find the channel in the hashtable.
32 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct lttng_ht
*ht
,
35 struct lttng_ht_node_str
*node
;
36 struct lttng_ht_iter iter
;
39 lttng_ht_lookup(ht
, (void *)name
, &iter
);
40 node
= lttng_ht_iter_get_node_str(&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 lttng_ht
*ht
,
62 struct lttng_ht_node_str
*node
;
63 struct lttng_ht_iter iter
;
66 lttng_ht_lookup(ht
, (void *) name
, &iter
);
67 node
= lttng_ht_iter_get_node_str(&iter
);
74 DBG2("Trace UST event found by name %s", name
);
76 return caa_container_of(node
, struct ltt_ust_event
, node
);
79 DBG2("Trace UST event NOT found by name %s", name
);
84 * Allocate and initialize a ust session data structure.
86 * Return pointer to structure or NULL.
88 struct ltt_ust_session
*trace_ust_create_session(char *path
,
89 unsigned int session_id
, struct lttng_domain
*domain
)
92 struct ltt_ust_session
*lus
;
94 /* Allocate a new ltt ust session */
95 lus
= zmalloc(sizeof(struct ltt_ust_session
));
97 PERROR("create ust session zmalloc");
101 /* Init data structure */
102 lus
->id
= session_id
;
103 lus
->start_trace
= 0;
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
);
109 /* Alloc UST global domain channels' HT */
110 lus
->domain_global
.channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
112 lus
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
113 if (lus
->consumer
== NULL
) {
118 * The tmp_consumer stays NULL until a set_consumer_uri command is
119 * executed. At this point, the consumer should be nullify until an
120 * enable_consumer command. This assignment is symbolic since we've zmalloc
123 lus
->tmp_consumer
= NULL
;
125 /* Use the default consumer output which is the tracing session path. */
126 ret
= snprintf(lus
->consumer
->dst
.trace_path
, PATH_MAX
, "%s/ust", path
);
128 PERROR("snprintf UST consumer trace path");
132 /* Set session path */
133 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
135 PERROR("snprintf kernel traces path");
136 goto error_free_session
;
139 DBG2("UST trace session create successful");
144 lttng_ht_destroy(lus
->domain_global
.channels
);
145 lttng_ht_destroy(lus
->domain_exec
);
146 lttng_ht_destroy(lus
->domain_pid
);
153 * Allocate and initialize a ust channel data structure.
155 * Return pointer to structure or NULL.
157 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
161 struct ltt_ust_channel
*luc
;
163 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
165 PERROR("ltt_ust_channel zmalloc");
169 /* Copy UST channel attributes */
170 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
171 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
172 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
173 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
174 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
175 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
177 /* Translate to UST output enum */
178 switch (luc
->attr
.output
) {
180 luc
->attr
.output
= LTTNG_UST_MMAP
;
184 /* Copy channel name */
185 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
186 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
189 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
190 /* Alloc hash tables */
191 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
192 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
194 /* Set trace output path */
195 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
197 PERROR("asprintf ust create channel");
198 goto error_free_channel
;
201 DBG2("Trace UST channel %s created", luc
->name
);
206 lttng_ht_destroy(luc
->ctx
);
207 lttng_ht_destroy(luc
->events
);
214 * Allocate and initialize a ust event. Set name and event type.
216 * Return pointer to structure or NULL.
218 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
220 struct ltt_ust_event
*lue
;
222 lue
= zmalloc(sizeof(struct ltt_ust_event
));
224 PERROR("ust event zmalloc");
229 case LTTNG_EVENT_PROBE
:
230 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
232 case LTTNG_EVENT_FUNCTION
:
233 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
235 case LTTNG_EVENT_FUNCTION_ENTRY
:
236 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
238 case LTTNG_EVENT_TRACEPOINT
:
239 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
242 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
243 goto error_free_event
;
246 /* Copy event name */
247 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
248 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
250 switch (ev
->loglevel_type
) {
251 case LTTNG_EVENT_LOGLEVEL_ALL
:
252 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
253 lue
->attr
.loglevel
= -1; /* Force to -1 */
255 case LTTNG_EVENT_LOGLEVEL_RANGE
:
256 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
257 lue
->attr
.loglevel
= ev
->loglevel
;
259 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
260 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
261 lue
->attr
.loglevel
= ev
->loglevel
;
264 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
265 goto error_free_event
;
270 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
271 /* Alloc context hash tables */
272 lue
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
273 if (lue
->ctx
== NULL
) {
274 ERR("Unable to create context hash table for event %s", ev
->name
);
275 goto error_free_event
;
278 DBG2("Trace UST event %s, loglevel (%d,%d) created",
279 lue
->attr
.name
, lue
->attr
.loglevel_type
,
291 * Allocate and initialize a ust metadata.
293 * Return pointer to structure or NULL.
295 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
298 struct ltt_ust_metadata
*lum
;
300 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
302 PERROR("ust metadata zmalloc");
306 /* Set default attributes */
307 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
308 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
309 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
310 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
311 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
312 lum
->attr
.output
= LTTNG_UST_MMAP
;
315 /* Set metadata trace path */
316 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
318 PERROR("asprintf ust metadata");
319 goto error_free_metadata
;
331 * Allocate and initialize an UST context.
333 * Return pointer to structure or NULL.
335 struct ltt_ust_context
*trace_ust_create_context(
336 struct lttng_event_context
*ctx
)
338 struct ltt_ust_context
*uctx
;
339 enum lttng_ust_context_type utype
;
342 case LTTNG_EVENT_CONTEXT_VTID
:
343 utype
= LTTNG_UST_CONTEXT_VTID
;
345 case LTTNG_EVENT_CONTEXT_VPID
:
346 utype
= LTTNG_UST_CONTEXT_VPID
;
348 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
349 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
351 case LTTNG_EVENT_CONTEXT_PROCNAME
:
352 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
355 ERR("Invalid UST context");
359 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
361 PERROR("zmalloc ltt_ust_context");
365 uctx
->ctx
.ctx
= utype
;
366 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
375 * RCU safe free context structure.
377 static void destroy_context_rcu(struct rcu_head
*head
)
379 struct lttng_ht_node_ulong
*node
=
380 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
381 struct ltt_ust_context
*ctx
=
382 caa_container_of(node
, struct ltt_ust_context
, node
);
388 * Cleanup UST context hash table.
390 static void destroy_contexts(struct lttng_ht
*ht
)
393 struct lttng_ht_node_ulong
*node
;
394 struct lttng_ht_iter iter
;
396 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
397 ret
= lttng_ht_del(ht
, &iter
);
399 call_rcu(&node
->head
, destroy_context_rcu
);
403 lttng_ht_destroy(ht
);
407 * Cleanup ust event structure.
409 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
411 DBG2("Trace destroy UST event %s", event
->attr
.name
);
412 destroy_contexts(event
->ctx
);
418 * URCU intermediate call to complete destroy event.
420 static void destroy_event_rcu(struct rcu_head
*head
)
422 struct lttng_ht_node_str
*node
=
423 caa_container_of(head
, struct lttng_ht_node_str
, head
);
424 struct ltt_ust_event
*event
=
425 caa_container_of(node
, struct ltt_ust_event
, node
);
427 trace_ust_destroy_event(event
);
431 * Cleanup UST events hashtable.
433 static void destroy_events(struct lttng_ht
*events
)
436 struct lttng_ht_node_str
*node
;
437 struct lttng_ht_iter iter
;
439 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
440 ret
= lttng_ht_del(events
, &iter
);
442 call_rcu(&node
->head
, destroy_event_rcu
);
445 lttng_ht_destroy(events
);
449 * Cleanup ust channel structure.
451 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
453 DBG2("Trace destroy UST channel %s", channel
->name
);
457 /* Destroying all events of the channel */
458 destroy_events(channel
->events
);
459 /* Destroying all context of the channel */
460 destroy_contexts(channel
->ctx
);
468 * URCU intermediate call to complete destroy channel.
470 static void destroy_channel_rcu(struct rcu_head
*head
)
472 struct lttng_ht_node_str
*node
=
473 caa_container_of(head
, struct lttng_ht_node_str
, head
);
474 struct ltt_ust_channel
*channel
=
475 caa_container_of(node
, struct ltt_ust_channel
, node
);
477 trace_ust_destroy_channel(channel
);
481 * Cleanup ust metadata structure.
483 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
485 if (!metadata
->handle
) {
488 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
493 * Iterate over a hash table containing channels and cleanup safely.
495 static void destroy_channels(struct lttng_ht
*channels
)
498 struct lttng_ht_node_str
*node
;
499 struct lttng_ht_iter iter
;
503 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
504 ret
= lttng_ht_del(channels
, &iter
);
506 call_rcu(&node
->head
, destroy_channel_rcu
);
509 lttng_ht_destroy(channels
);
515 * Cleanup UST pid domain.
517 static void destroy_domain_pid(struct lttng_ht
*ht
)
520 struct lttng_ht_iter iter
;
521 struct ltt_ust_domain_pid
*dpid
;
523 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
524 ret
= lttng_ht_del(ht
, &iter
);
526 destroy_channels(dpid
->channels
);
529 lttng_ht_destroy(ht
);
533 * Cleanup UST exec name domain.
535 static void destroy_domain_exec(struct lttng_ht
*ht
)
538 struct lttng_ht_iter iter
;
539 struct ltt_ust_domain_exec
*dexec
;
541 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
542 ret
= lttng_ht_del(ht
, &iter
);
544 destroy_channels(dexec
->channels
);
547 lttng_ht_destroy(ht
);
551 * Cleanup UST global domain.
553 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
555 destroy_channels(dom
->channels
);
559 * Cleanup ust session structure
561 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
563 /* Extra protection */
564 if (session
== NULL
) {
570 DBG2("Trace UST destroy session %u", session
->id
);
572 /* Cleaning up UST domain */
573 destroy_domain_global(&session
->domain_global
);
574 destroy_domain_pid(session
->domain_pid
);
575 destroy_domain_exec(session
->domain_exec
);