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.
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 all channel.
36 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
37 struct lttng_kernel_context
*kctx
)
40 struct ltt_kernel_channel
*kchan
;
42 DBG("Adding kernel context to all channels");
44 /* Go over all channels */
45 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
46 ret
= kernel_add_channel_context(kchan
, kctx
);
48 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
60 * Add kernel context to a specific channel.
62 static int add_kctx_to_channel(struct lttng_kernel_context
*kctx
,
63 struct ltt_kernel_channel
*kchan
)
67 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
69 ret
= kernel_add_channel_context(kchan
, kctx
);
71 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
82 * Add UST context to channel.
84 static int add_uctx_to_channel(struct ltt_ust_session
*usess
, int domain
,
85 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
88 struct ltt_ust_context
*uctx
;
89 struct lttng_ht_iter iter
;
90 struct lttng_ht_node_ulong
*uctx_node
;
92 /* Create ltt UST context */
93 uctx
= trace_ust_create_context(ctx
);
100 case LTTNG_DOMAIN_UST
:
101 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
111 /* Lookup context before adding it */
112 lttng_ht_lookup(uchan
->ctx
, (void *)((unsigned long)uctx
->ctx
.ctx
), &iter
);
113 uctx_node
= lttng_ht_iter_get_node_ulong(&iter
);
114 if (uctx_node
!= NULL
) {
119 /* Add ltt UST context node to ltt UST channel */
120 lttng_ht_add_unique_ulong(uchan
->ctx
, &uctx
->node
);
122 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
132 * Add kernel context to tracer.
134 int context_kernel_add(struct ltt_kernel_session
*ksession
,
135 struct lttng_event_context
*ctx
, char *channel_name
)
138 struct ltt_kernel_channel
*kchan
;
139 struct lttng_kernel_context kctx
;
141 /* Setup kernel context structure */
143 case LTTNG_EVENT_CONTEXT_PID
:
144 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
146 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
147 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_COUNTER
;
149 case LTTNG_EVENT_CONTEXT_PROCNAME
:
150 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
152 case LTTNG_EVENT_CONTEXT_PRIO
:
153 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
155 case LTTNG_EVENT_CONTEXT_NICE
:
156 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
158 case LTTNG_EVENT_CONTEXT_VPID
:
159 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
161 case LTTNG_EVENT_CONTEXT_TID
:
162 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
164 case LTTNG_EVENT_CONTEXT_VTID
:
165 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
167 case LTTNG_EVENT_CONTEXT_PPID
:
168 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
170 case LTTNG_EVENT_CONTEXT_VPPID
:
171 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
173 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
174 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
177 return LTTNG_ERR_KERN_CONTEXT_FAIL
;
180 kctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
181 kctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
182 strncpy(kctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
183 LTTNG_SYMBOL_NAME_LEN
);
184 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
186 if (strlen(channel_name
) == 0) {
187 ret
= add_kctx_all_channels(ksession
, &kctx
);
188 if (ret
!= LTTNG_OK
) {
192 /* Get kernel channel */
193 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
195 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
199 ret
= add_kctx_to_channel(&kctx
, kchan
);
200 if (ret
!= LTTNG_OK
) {
212 * Add UST context to tracer.
214 int context_ust_add(struct ltt_ust_session
*usess
, int domain
,
215 struct lttng_event_context
*ctx
, char *channel_name
)
218 struct lttng_ht_iter iter
;
219 struct lttng_ht
*chan_ht
;
220 struct ltt_ust_channel
*uchan
= NULL
;
223 * Define which channel's hashtable to use from the domain or quit if
227 case LTTNG_DOMAIN_UST
:
228 chan_ht
= usess
->domain_global
.channels
;
231 case LTTNG_DOMAIN_UST_EXEC_NAME
:
232 case LTTNG_DOMAIN_UST_PID
:
233 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
240 /* Get UST channel if defined */
241 if (strlen(channel_name
) != 0) {
242 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
244 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
250 /* Add ctx to channel */
251 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
253 /* Add ctx all events, all channels */
254 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
255 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
257 ERR("Context failed for channel %s", uchan
->name
);
265 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
268 ret
= LTTNG_ERR_FATAL
;
271 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
274 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;