2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <urcu/list.h>
26 #include <common/error.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "trace-ust.h"
36 * Add kernel context to all channel.
38 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
39 struct ltt_kernel_context
*kctx
)
42 struct ltt_kernel_channel
*kchan
;
47 DBG("Adding kernel context to all channels");
49 /* Go over all channels */
50 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
51 ret
= kernel_add_channel_context(kchan
, kctx
);
64 * Add kernel context to a specific channel.
66 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
67 struct ltt_kernel_channel
*kchan
)
74 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
76 ret
= kernel_add_channel_context(kchan
, kctx
);
88 * Add UST context to channel.
90 static int add_uctx_to_channel(struct ltt_ust_session
*usess
,
91 enum lttng_domain_type domain
,
92 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
95 struct ltt_ust_context
*uctx
= NULL
;
101 /* Check if context is duplicate */
102 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
103 if (trace_ust_match_context(uctx
, ctx
)) {
111 case LTTNG_DOMAIN_JUL
:
112 case LTTNG_DOMAIN_LOG4J
:
116 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
117 /* Other contexts are not needed by the agent. */
120 agt
= trace_ust_find_agent(usess
, domain
);
123 agt
= agent_create(domain
);
125 ret
= LTTNG_ERR_NOMEM
;
128 agent_add(agt
, usess
->agents
);
130 ret
= agent_add_context(ctx
, agt
);
131 if (ret
!= LTTNG_OK
) {
135 ret
= agent_enable_context(ctx
, domain
);
136 if (ret
!= LTTNG_OK
) {
141 case LTTNG_DOMAIN_UST
:
147 /* Create ltt UST context */
148 uctx
= trace_ust_create_context(ctx
);
154 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
161 /* Add ltt UST context node to ltt UST channel */
162 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
164 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
166 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
177 * Add kernel context to tracer.
179 int context_kernel_add(struct ltt_kernel_session
*ksession
,
180 struct lttng_event_context
*ctx
, char *channel_name
)
183 struct ltt_kernel_channel
*kchan
;
184 struct ltt_kernel_context
*kctx
;
188 assert(channel_name
);
190 kctx
= trace_kernel_create_context(NULL
);
192 ret
= LTTNG_ERR_NOMEM
;
196 /* Setup kernel context structure */
198 case LTTNG_EVENT_CONTEXT_PID
:
199 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
201 case LTTNG_EVENT_CONTEXT_PROCNAME
:
202 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
204 case LTTNG_EVENT_CONTEXT_PRIO
:
205 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
207 case LTTNG_EVENT_CONTEXT_NICE
:
208 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
210 case LTTNG_EVENT_CONTEXT_VPID
:
211 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
213 case LTTNG_EVENT_CONTEXT_TID
:
214 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
216 case LTTNG_EVENT_CONTEXT_VTID
:
217 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
219 case LTTNG_EVENT_CONTEXT_PPID
:
220 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
222 case LTTNG_EVENT_CONTEXT_VPPID
:
223 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
225 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
226 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
228 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
229 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
230 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
;
232 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
:
233 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
;
235 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE
:
236 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
;
238 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
:
239 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
;
241 case LTTNG_EVENT_CONTEXT_MIGRATABLE
:
242 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_MIGRATABLE
;
245 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
249 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
250 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
251 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
252 LTTNG_SYMBOL_NAME_LEN
);
253 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
255 if (*channel_name
== '\0') {
256 ret
= add_kctx_all_channels(ksession
, kctx
);
257 if (ret
!= LTTNG_OK
) {
261 /* Get kernel channel */
262 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
264 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
268 ret
= add_kctx_to_channel(kctx
, kchan
);
269 if (ret
!= LTTNG_OK
) {
278 trace_kernel_destroy_context(kctx
);
284 * Add UST context to tracer.
286 int context_ust_add(struct ltt_ust_session
*usess
,
287 enum lttng_domain_type domain
, struct lttng_event_context
*ctx
,
291 struct lttng_ht_iter iter
;
292 struct lttng_ht
*chan_ht
;
293 struct ltt_ust_channel
*uchan
= NULL
;
297 assert(channel_name
);
301 chan_ht
= usess
->domain_global
.channels
;
303 /* Get UST channel if defined */
304 if (channel_name
[0] != '\0') {
305 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
307 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
313 /* Add ctx to channel */
314 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
317 /* Add ctx all events, all channels */
318 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
319 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
321 ERR("Failed to add context to channel %s",
331 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
334 ret
= LTTNG_ERR_FATAL
;
337 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
340 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;