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 if (path
&& strlen(path
) > 0) {
127 ret
= snprintf(lus
->consumer
->dst
.trace_path
, PATH_MAX
,
128 "%s" DEFAULT_UST_TRACE_DIR
, path
);
130 PERROR("snprintf UST consumer trace path");
134 /* Set session path */
135 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s" DEFAULT_UST_TRACE_DIR
,
138 PERROR("snprintf kernel traces path");
143 DBG2("UST trace session create successful");
148 consumer_destroy_output(lus
->consumer
);
150 lttng_ht_destroy(lus
->domain_global
.channels
);
151 lttng_ht_destroy(lus
->domain_exec
);
152 lttng_ht_destroy(lus
->domain_pid
);
159 * Allocate and initialize a ust channel data structure.
161 * Return pointer to structure or NULL.
163 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
167 struct ltt_ust_channel
*luc
;
169 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
171 PERROR("ltt_ust_channel zmalloc");
175 /* Copy UST channel attributes */
176 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
177 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
178 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
179 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
180 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
181 luc
->attr
.output
= (enum lttng_ust_output
) chan
->attr
.output
;
183 /* Translate to UST output enum */
184 switch (luc
->attr
.output
) {
186 luc
->attr
.output
= LTTNG_UST_MMAP
;
190 /* Copy channel name */
191 strncpy(luc
->name
, chan
->name
, sizeof(luc
->name
));
192 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
195 lttng_ht_node_init_str(&luc
->node
, luc
->name
);
196 /* Alloc hash tables */
197 luc
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
198 luc
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
200 /* Set trace output path */
201 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
203 PERROR("asprintf ust create channel");
204 goto error_free_channel
;
207 DBG2("Trace UST channel %s created", luc
->name
);
212 lttng_ht_destroy(luc
->ctx
);
213 lttng_ht_destroy(luc
->events
);
220 * Allocate and initialize a ust event. Set name and event type.
222 * Return pointer to structure or NULL.
224 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
226 struct ltt_ust_event
*lue
;
228 lue
= zmalloc(sizeof(struct ltt_ust_event
));
230 PERROR("ust event zmalloc");
235 case LTTNG_EVENT_PROBE
:
236 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
238 case LTTNG_EVENT_FUNCTION
:
239 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
241 case LTTNG_EVENT_FUNCTION_ENTRY
:
242 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
244 case LTTNG_EVENT_TRACEPOINT
:
245 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
248 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
249 goto error_free_event
;
252 /* Copy event name */
253 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
254 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
256 switch (ev
->loglevel_type
) {
257 case LTTNG_EVENT_LOGLEVEL_ALL
:
258 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
259 lue
->attr
.loglevel
= -1; /* Force to -1 */
261 case LTTNG_EVENT_LOGLEVEL_RANGE
:
262 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
263 lue
->attr
.loglevel
= ev
->loglevel
;
265 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
266 lue
->attr
.loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
267 lue
->attr
.loglevel
= ev
->loglevel
;
270 ERR("Unknown ust loglevel type (%d)", ev
->loglevel_type
);
271 goto error_free_event
;
276 lttng_ht_node_init_str(&lue
->node
, lue
->attr
.name
);
277 /* Alloc context hash tables */
278 lue
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
279 if (lue
->ctx
== NULL
) {
280 ERR("Unable to create context hash table for event %s", ev
->name
);
281 goto error_free_event
;
284 DBG2("Trace UST event %s, loglevel (%d,%d) created",
285 lue
->attr
.name
, lue
->attr
.loglevel_type
,
297 * Allocate and initialize a ust metadata.
299 * Return pointer to structure or NULL.
301 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
304 struct ltt_ust_metadata
*lum
;
306 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
308 PERROR("ust metadata zmalloc");
312 /* Set default attributes */
313 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
314 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
315 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
316 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
317 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
318 lum
->attr
.output
= LTTNG_UST_MMAP
;
321 /* Set metadata trace path */
322 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
324 PERROR("asprintf ust metadata");
325 goto error_free_metadata
;
337 * Allocate and initialize an UST context.
339 * Return pointer to structure or NULL.
341 struct ltt_ust_context
*trace_ust_create_context(
342 struct lttng_event_context
*ctx
)
344 struct ltt_ust_context
*uctx
;
345 enum lttng_ust_context_type utype
;
348 case LTTNG_EVENT_CONTEXT_VTID
:
349 utype
= LTTNG_UST_CONTEXT_VTID
;
351 case LTTNG_EVENT_CONTEXT_VPID
:
352 utype
= LTTNG_UST_CONTEXT_VPID
;
354 case LTTNG_EVENT_CONTEXT_PTHREAD_ID
:
355 utype
= LTTNG_UST_CONTEXT_PTHREAD_ID
;
357 case LTTNG_EVENT_CONTEXT_PROCNAME
:
358 utype
= LTTNG_UST_CONTEXT_PROCNAME
;
361 ERR("Invalid UST context");
365 uctx
= zmalloc(sizeof(struct ltt_ust_context
));
367 PERROR("zmalloc ltt_ust_context");
371 uctx
->ctx
.ctx
= utype
;
372 lttng_ht_node_init_ulong(&uctx
->node
, (unsigned long) uctx
->ctx
.ctx
);
381 * RCU safe free context structure.
383 static void destroy_context_rcu(struct rcu_head
*head
)
385 struct lttng_ht_node_ulong
*node
=
386 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
387 struct ltt_ust_context
*ctx
=
388 caa_container_of(node
, struct ltt_ust_context
, node
);
394 * Cleanup UST context hash table.
396 static void destroy_contexts(struct lttng_ht
*ht
)
399 struct lttng_ht_node_ulong
*node
;
400 struct lttng_ht_iter iter
;
402 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, node
, node
) {
403 ret
= lttng_ht_del(ht
, &iter
);
405 call_rcu(&node
->head
, destroy_context_rcu
);
409 lttng_ht_destroy(ht
);
413 * Cleanup ust event structure.
415 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
417 DBG2("Trace destroy UST event %s", event
->attr
.name
);
418 destroy_contexts(event
->ctx
);
424 * URCU intermediate call to complete destroy event.
426 static void destroy_event_rcu(struct rcu_head
*head
)
428 struct lttng_ht_node_str
*node
=
429 caa_container_of(head
, struct lttng_ht_node_str
, head
);
430 struct ltt_ust_event
*event
=
431 caa_container_of(node
, struct ltt_ust_event
, node
);
433 trace_ust_destroy_event(event
);
437 * Cleanup UST events hashtable.
439 static void destroy_events(struct lttng_ht
*events
)
442 struct lttng_ht_node_str
*node
;
443 struct lttng_ht_iter iter
;
445 cds_lfht_for_each_entry(events
->ht
, &iter
.iter
, node
, node
) {
446 ret
= lttng_ht_del(events
, &iter
);
448 call_rcu(&node
->head
, destroy_event_rcu
);
451 lttng_ht_destroy(events
);
455 * Cleanup ust channel structure.
457 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
459 DBG2("Trace destroy UST channel %s", channel
->name
);
463 /* Destroying all events of the channel */
464 destroy_events(channel
->events
);
465 /* Destroying all context of the channel */
466 destroy_contexts(channel
->ctx
);
474 * URCU intermediate call to complete destroy channel.
476 static void destroy_channel_rcu(struct rcu_head
*head
)
478 struct lttng_ht_node_str
*node
=
479 caa_container_of(head
, struct lttng_ht_node_str
, head
);
480 struct ltt_ust_channel
*channel
=
481 caa_container_of(node
, struct ltt_ust_channel
, node
);
483 trace_ust_destroy_channel(channel
);
487 * Cleanup ust metadata structure.
489 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
491 if (!metadata
->handle
) {
494 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
499 * Iterate over a hash table containing channels and cleanup safely.
501 static void destroy_channels(struct lttng_ht
*channels
)
504 struct lttng_ht_node_str
*node
;
505 struct lttng_ht_iter iter
;
509 cds_lfht_for_each_entry(channels
->ht
, &iter
.iter
, node
, node
) {
510 ret
= lttng_ht_del(channels
, &iter
);
512 call_rcu(&node
->head
, destroy_channel_rcu
);
515 lttng_ht_destroy(channels
);
521 * Cleanup UST pid domain.
523 static void destroy_domain_pid(struct lttng_ht
*ht
)
526 struct lttng_ht_iter iter
;
527 struct ltt_ust_domain_pid
*dpid
;
529 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dpid
, node
.node
) {
530 ret
= lttng_ht_del(ht
, &iter
);
532 destroy_channels(dpid
->channels
);
535 lttng_ht_destroy(ht
);
539 * Cleanup UST exec name domain.
541 static void destroy_domain_exec(struct lttng_ht
*ht
)
544 struct lttng_ht_iter iter
;
545 struct ltt_ust_domain_exec
*dexec
;
547 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, dexec
, node
.node
) {
548 ret
= lttng_ht_del(ht
, &iter
);
550 destroy_channels(dexec
->channels
);
553 lttng_ht_destroy(ht
);
557 * Cleanup UST global domain.
559 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
561 destroy_channels(dom
->channels
);
565 * Cleanup ust session structure
567 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
569 /* Extra protection */
570 if (session
== NULL
) {
576 DBG2("Trace UST destroy session %u", session
->id
);
578 /* Cleaning up UST domain */
579 destroy_domain_global(&session
->domain_global
);
580 destroy_domain_pid(session
->domain_pid
);
581 destroy_domain_exec(session
->domain_exec
);
583 consumer_destroy_output(session
->consumer
);
584 consumer_destroy_output(session
->tmp_consumer
);