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.
23 #include <urcu/list.h>
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
31 #include "trace-ust.h"
34 * Add kernel context to an event of a specific channel.
36 static int add_kctx_to_event(struct lttng_kernel_context
*kctx
,
37 struct ltt_kernel_channel
*kchan
, char *event_name
)
40 struct ltt_kernel_event
*kevent
;
42 DBG("Add kernel context to event %s", event_name
);
44 kevent
= trace_kernel_get_event_by_name(event_name
, kchan
);
46 ret
= kernel_add_event_context(kevent
, kctx
);
60 * Add kernel context to all channel.
62 * If event_name is specified, add context to event instead.
64 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
65 struct lttng_kernel_context
*kctx
, char *event_name
)
67 int ret
, no_event
= 0, found
= 0;
68 struct ltt_kernel_channel
*kchan
;
70 if (strlen(event_name
) == 0) {
74 DBG("Adding kernel context to all channels (event: %s)", event_name
);
76 /* Go over all channels */
77 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
79 ret
= kernel_add_channel_context(kchan
, kctx
);
81 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
85 ret
= add_kctx_to_event(kctx
, kchan
, event_name
);
87 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
89 } else if (ret
== 1) {
90 /* Event found and context added */
97 if (!found
&& !no_event
) {
98 ret
= LTTCOMM_NO_EVENT
;
109 * Add kernel context to a specific channel.
111 * If event_name is specified, add context to that event.
113 static int add_kctx_to_channel(struct lttng_kernel_context
*kctx
,
114 struct ltt_kernel_channel
*kchan
, char *event_name
)
116 int ret
, no_event
= 0, found
= 0;
118 if (strlen(event_name
) == 0) {
122 DBG("Add kernel context to channel '%s', event '%s'",
123 kchan
->channel
->name
, event_name
);
126 ret
= kernel_add_channel_context(kchan
, kctx
);
128 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
132 ret
= add_kctx_to_event(kctx
, kchan
, event_name
);
134 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
136 } else if (ret
== 1) {
137 /* Event found and context added */
142 if (!found
&& !no_event
) {
143 ret
= LTTCOMM_NO_EVENT
;
154 * Add UST context to channel.
156 static int add_uctx_to_channel(struct ltt_ust_session
*usess
, int domain
,
157 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
160 struct ltt_ust_context
*uctx
;
162 /* Create ltt UST context */
163 uctx
= trace_ust_create_context(ctx
);
170 case LTTNG_DOMAIN_UST
:
171 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
181 /* Add ltt UST context node to ltt UST channel */
182 lttng_ht_add_unique_ulong(uchan
->ctx
, &uctx
->node
);
192 * Add UST context to event.
194 static int add_uctx_to_event(struct ltt_ust_session
*usess
, int domain
,
195 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
196 struct lttng_event_context
*ctx
)
199 struct ltt_ust_context
*uctx
;
201 /* Create ltt UST context */
202 uctx
= trace_ust_create_context(ctx
);
209 case LTTNG_DOMAIN_UST
:
210 ret
= ust_app_add_ctx_event_glb(usess
, uchan
, uevent
, uctx
);
220 /* Add ltt UST context node to ltt UST event */
221 lttng_ht_add_unique_ulong(uevent
->ctx
, &uctx
->node
);
231 * Add kernel context to tracer.
233 int context_kernel_add(struct ltt_kernel_session
*ksession
,
234 struct lttng_event_context
*ctx
, char *event_name
,
238 struct ltt_kernel_channel
*kchan
;
239 struct lttng_kernel_context kctx
;
241 /* Setup kernel context structure */
243 case LTTNG_EVENT_CONTEXT_PID
:
244 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
246 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
247 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_COUNTER
;
249 case LTTNG_EVENT_CONTEXT_PROCNAME
:
250 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
252 case LTTNG_EVENT_CONTEXT_PRIO
:
253 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
255 case LTTNG_EVENT_CONTEXT_NICE
:
256 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
258 case LTTNG_EVENT_CONTEXT_VPID
:
259 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
261 case LTTNG_EVENT_CONTEXT_TID
:
262 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
264 case LTTNG_EVENT_CONTEXT_VTID
:
265 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
267 case LTTNG_EVENT_CONTEXT_PPID
:
268 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
270 case LTTNG_EVENT_CONTEXT_VPPID
:
271 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
274 return LTTCOMM_KERN_CONTEXT_FAIL
;
277 kctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
278 kctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
279 strncpy(kctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
280 LTTNG_SYMBOL_NAME_LEN
);
281 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
283 if (strlen(channel_name
) == 0) {
284 ret
= add_kctx_all_channels(ksession
, &kctx
, event_name
);
285 if (ret
!= LTTCOMM_OK
) {
289 /* Get kernel channel */
290 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
292 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
296 ret
= add_kctx_to_channel(&kctx
, kchan
, event_name
);
297 if (ret
!= LTTCOMM_OK
) {
309 * Add UST context to tracer.
311 int context_ust_add(struct ltt_ust_session
*usess
, int domain
,
312 struct lttng_event_context
*ctx
, char *event_name
,
315 int ret
= LTTCOMM_OK
, have_event
= 0;
316 struct lttng_ht_iter iter
, uiter
;
317 struct lttng_ht
*chan_ht
;
318 struct ltt_ust_channel
*uchan
= NULL
;
319 struct ltt_ust_event
*uevent
= NULL
;
322 * Define which channel's hashtable to use from the domain or quit if
326 case LTTNG_DOMAIN_UST
:
327 chan_ht
= usess
->domain_global
.channels
;
330 case LTTNG_DOMAIN_UST_EXEC_NAME
:
331 case LTTNG_DOMAIN_UST_PID
:
332 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
339 /* Do we have an event name */
340 if (strlen(event_name
) != 0) {
344 /* Get UST channel if defined */
345 if (strlen(channel_name
) != 0) {
346 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
348 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
353 /* If UST channel specified and event name, get UST event ref */
354 if (uchan
&& have_event
) {
355 uevent
= trace_ust_find_event_by_name(uchan
->events
, event_name
);
356 if (uevent
== NULL
) {
357 ret
= LTTCOMM_UST_EVENT_NOT_FOUND
;
362 /* At this point, we have 4 possibilities */
364 if (uchan
&& uevent
) { /* Add ctx to event in channel */
365 ret
= add_uctx_to_event(usess
, domain
, uchan
, uevent
, ctx
);
366 } else if (uchan
&& !have_event
) { /* Add ctx to channel */
367 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
368 } else if (!uchan
&& have_event
) { /* Add ctx to event */
369 /* Add context to event without having the channel name */
370 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
371 uevent
= trace_ust_find_event_by_name(uchan
->events
, event_name
);
372 if (uevent
!= NULL
) {
373 ret
= add_uctx_to_event(usess
, domain
, uchan
, uevent
, ctx
);
375 * LTTng UST does not allowed the same event to be registered
376 * multiple time in different or the same channel. So, if we
377 * found our event, we stop.
382 ret
= LTTCOMM_UST_EVENT_NOT_FOUND
;
384 } else if (!uchan
&& !have_event
) { /* Add ctx all events, all channels */
385 /* For all channels */
386 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
387 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
389 ERR("Context added to channel %s failed", uchan
->name
);
393 /* For all events in channel */
394 cds_lfht_for_each_entry(uchan
->events
->ht
, &uiter
.iter
, uevent
,
396 ret
= add_uctx_to_event(usess
, domain
, uchan
, uevent
, ctx
);
398 ERR("Context add to event %s in channel %s failed",
399 uevent
->attr
.name
, uchan
->name
);
409 ret
= LTTCOMM_UST_CONTEXT_EXIST
;